Highcharts windbarb out-of-memory crash - reactjs

I'm trying to use the modules windbarb in a react project.
On render, chrome pauses the process with a "paused before potential out-of-memory crash" highlighting line 135 in the windbarb.js file.
this is my code:
import React, { Component } from 'react';
import Highcharts from 'highcharts'
import HighchartsReact from 'highcharts-react-official'
import WindBarbs from 'highcharts/modules/windbarb'
WindBarbs(Highcharts)
class Test extends Component {
constructor(props) {
super(props)
this.state = {
lineData: [
[1569243600000, 12.5],
[1569247200000, 12.2]
],
windData: [
[1569243600000, 29.6],
[1569247200000, 26.5]
],
highcharts: Highcharts
}
}
render(){
let options = {
series: [{
name: 'Temperature',
type: 'line',
data: this.state.lineData
}, {
name: 'Wind direction',
type: 'windbarb',
data: this.state.windData
}]
}
return(
<HighchartsReact
highcharts={this.state.highcharts}
options={options}
/>
)
}
}
export default Test

That problem is related to your data. First elements from the data array by default are used as values for windbard series which causes very complex svg rendering. As a solution you can use keys option.
series: [{
type: 'line',
data: [
[1569243600000, 12.5],
[1569247200000, 12.2]
]
}, {
type: 'windbarb',
keys: ['x', 'value'],
data: [
[1569243600000, 29.6],
[1569247200000, 26.5]
]
}]
Live demo: https://jsfiddle.net/BlackLabel/0k8vmzfd/
API Reference:
https://api.highcharts.com/highcharts/series.windbarb.data
https://api.highcharts.com/highcharts/series.windbarb.keys

Related

Rendering data in React Native

I have an object named data that contains information about book. The simplest thing I want to do is render the book name. I am trying like below. Not working.
const data = {
"book": {
"id": "2d320",
"name": "book 1",
"Chapters": [
{
"No": "1",
"date": "2010-04-22",
"plots": [
{
"name": "Hero"
}
]
},
{
"No": "2",
"date": "2010-04-22",
"plots": [
{
"name": "Heroine"
}
]
},
]}}
export class Books {
render() {
return (
<View>
<div>{data.book.name}</div>
</View>
);
}
}
div should not be used in react-native, just replace it with Text after importing it. Secondly, class-based components should extend React.Component like so
import React, { Component } from 'react';
import { Text, View } from 'react-native';
const data = {
book: {
id: '2d320',
name: 'book 1',
Chapters: [
{
No: '1',
date: '2010-04-22',
plots: [
{
name: 'Hero',
},
],
},
{
No: '2',
date: '2010-04-22',
plots: [
{
name: 'Heroine',
},
],
},
],
},
};
class Book extends Component {
render() {
return <View>
<Text>{data.book.name}</Text>
</View>;
}
}
export default Book;
Here is a snack displaying the output you want.
If you don't want to import Component from 'react' and like to export directly then may also use this syntax
export default class Book extends React.Component {
render() {
return (
<View>
<Text>{data.book.name}</Text>
</View>
);
}
}

Create a custom element in EditorJs

I added EditorJs plugin in my react js application:
import ReactDOM from "react-dom";
import React, { Component } from "react";
import EditorJs from "react-editor-js";
import { EDITOR_JS_TOOLS } from "./constants";
class ReactEditor extends Component {
render() {
return (
<EditorJs
tools={EDITOR_JS_TOOLS}
data={{
blocks: [
{
type: "header",
data: {
text: "Editor.js",
level: 2
}
},
{
type: "paragraph",
data: {
}
},
{
type: "header",
data: {
text: "Key features",
level: 3
}
},
{
type: "list",
data: {
style: "unordered",
items: [
"It is a block-styled editor",
"It returns clean data output in JSON",
"Designed to be extendable and pluggable with a simple API"
]
}
},
{
type: "header",
data: {
text: "What does it mean «block-styled editor»",
level: 3
}
},
{
type: "paragraph",
data: {
text:
'Workspace in classic editors is made of a single contenteditable element, used to create different HTML markups. Editor.js <mark class="cdx-marker">workspace consists of separate Blocks: paragraphs, headings, images, lists, quotes, etc</mark>. Each of them is an independent contenteditable element (or more complex structure) provided by Plugin and united by Editor\'s Core.'
}
},
{
type: "paragraph",
data: {
text:
'There are dozens of ready-to-use Blocks and the simple API for creation any Block you need. For example, you can implement Blocks for Tweets, Instagram posts, surveys and polls, CTA-buttons and even games.'
}
},
{
type: "header",
data: {
text: "What does it mean clean data output",
level: 3
}
},
{
type: "paragraph",
data: {
text:
"Classic WYSIWYG-editors produce raw HTML-markup with both content data and content appearance. On the contrary, Editor.js outputs JSON object with data of each Block. You can see an example below"
}
},
{
type: "paragraph",
data: {
text:
'Given data can be used as you want: render with HTML for <code class="inline-code">Web clients</code>, render natively for <code class="inline-code">mobile apps</code>, create markup for <code class="inline-code">Facebook Instant Articles</code> or <code class="inline-code">Google AMP</code>, generate an <code class="inline-code">audio version</code> and so on.'
}
},
{
type: "paragraph",
data: {
text:
"Clean data is useful to sanitize, validate and process on the backend."
}
},
{
type: "delimiter",
data: {}
},
{
type: "paragraph",
data: {
text:
"We have been working on this project more than three years. Several large media projects help us to test and debug the Editor, to make it's core more stable. At the same time we significantly improved the API. Now, it can be used to create any plugin for any task. Hope you enjoy. 😏"
}
},
{
type: "image",
data: {
file: {
url:
"https://codex.so/upload/redactor_images/o_e48549d1855c7fc1807308dd14990126.jpg"
},
caption: "",
withBorder: true,
stretched: false,
withBackground: false
}
}
],
version: "2.12.4"
}}
/>
);
}
}
ReactDOM.render(<ReactEditor />, document.getElementById("root"));
According to the documentation i can create a custom element:
render() {
return (
<EditorJs holder="custom">
<div id="custom" />
</EditorJs>
);
}
Question: I want to add as a custom element an input: <input type="text"/>, but i don't manage even if i do:
<EditorJs holder="custom">
<input id="custom" type="text"/>
</EditorJs>
Who knows how to add this custom element in the plugin above?
demo: https://codesandbox.io/embed/react-editor-js-23opz
I found in the documentation that i can create a plugin for editor.js:
https://editorjs.io/the-first-plugin. One of example looks like this:
class SimpleImage {
static get toolbox() {
return {
title: 'Image',
icon: '<svg width="17" height="15" viewBox="0 0 336 276" xmlns="http://www.w3.org/2000/svg"><path d="M291 150V79c0-19-15-34-34-34H79c-19 0-34 15-34 34v42l67-44 81 72 56-29 42 30zm0 52l-43-30-56 30-81-67-66 39v23c0 19 15 34 34 34h178c17 0 31-13 34-29zM79 0h178c44 0 79 35 79 79v118c0 44-35 79-79 79H79c-44 0-79-35-79-79V79C0 35 35 0 79 0z"/></svg>'
};
}
render() {
return document.createElement('input');
}
save(blockContent) {
return {
url: blockContent.value
}
}
}

Apollo Client - How to test a component that uses multiple queries, using HOC components that use compose

I am reading over the docs for testing React/Apollo components Link. If the component has one query, it seems pretty simple to test it.
const mocks = [
{
request: {
query: GET_DOG_QUERY,
variables: {
name: 'Buck',
},
},
result: {
data: {
dog: { id: '1', name: 'Buck', breed: 'bulldog' },
},
},
},
];
it('renders without error', () => {
renderer.create(
<MockedProvider mocks={mocks} addTypename={false}>
<Dog name="Buck" />
</MockedProvider>,
);
});
My component is a little different than the one provided in the documentation.
It doesn't use the useQuery hook, instead I am opting for the HOC approach as outlined here.
I have two queries that my function uses, and so I use two graphql functions and combine them together using compose, as recommended in the docs.
My component is exported like this:
export default compose(withQueryA, withQueryB)(MyComponent);
const withQueryA = graphql(QUERY_A, {
name: "QueryA",
options: (props) => ({
variables: {
foo: props.foo,
},
}),
});
const withQueryB = graphql(QUERY_B, {
name: "QueryB ",
options: (props) => ({
variables: {
bar: props.bar,
},
}),
});
What I'm trying to do is provide the mocks object with multiple objects, each containing a request/result for the corresponding query. I just wanted to know if anyone has been testing their components in a similar way or if there is a better suggestion.
const mocks = [
{
request: {
query: QUERY_A,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
{
request: {
query: QUERY_B,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
];
I'm also confused about what to put in the result object. When I console.log what is actually returned to the component when making a query in production, it has the data plus error, fetchMore, loading, networkStatus. Do I have to put all those things in the mocks as well?
My feeling was correct. The result object should look something like this:
const mocks = [
{
request: {
query: QUERY_A,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
{
request: {
query: QUERY_B,
variables: {
foo: "bar",
},
},
result: {
data: {
...,
},
},
},
];

How to create multiple line charts from a single component in React

I am trying to create multiple line charts from a single component in react and seem to be getting lost a big.
My API is giving me back data that looks like this.
[
{
"price": 6500,
"expiration": "10",
"bid": 86.44,
"mark": 89.74,
"ask": 95.94,
"dif": -0.01575
},
{
"price": 6750,
"expiration": "10",
"bid": 74.09,
"mark": 78.77,
"ask": 82.52,
"dif": -0.02023
},
{
"price": 7000,
"expiration": "20",
"bid": 69.28,
"mark": 70.67,
"ask": 74.62,
"dif": -0.03171
},
{
"price": 7250,
"expiration": "20",
"bid": 68.06,
"mark": 67.44,
"ask": 71.08,
"dif": -0.06388
}
]
What I am trying to do is to create a component that will take this data and map it to two separate line charts using Chart.Js.
Each chart should be broken out by expiration. With the X-axis being price and the Y-axis is supposed to be mark. I have found a good example implementing close to what I want to do here --> How to map multiple charts with chart.js in react
But I want to create 2 charts from this data. Where the charts are grouped by Expiration. I am not sure what I am missing from the example above that I would need to change to do this. Any help would be amazing.
import React, { Component } from "react";
import ReactDOM from "react-dom";
import { Line } from "react-chartjs-2";
import axios from "axios";
export default class Chart extends Component {
constructor(props) {
super(props);
this.state = {
chartData: [],
data: []
};
}
componentDidMount() {
fetch(API, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({query: query}),
})
.then(res => res.json())
.then(
(res)=>{
const x = res.data;
let chartData = [];
x.forEach(element => {
chartData.push({
labels: element.expiration,
datasets: [{ data: element.price}]
});
});
this.setState({ chartData });
});
}
render() {
return (
<div className="chart">
{this.state.chartData.map((n, index) => {
return <Line key={index} data={n} />;
})}
</div>
);
}
}

Use external JSON file for typeahead instead of .TS file

I got the Typeahead working using Angular 8 and ng-bootstrap. However, I would like the empId to come from a JSON file as there's over 10,000 entries. Currently, I have this:
<input type="text" [(ngModel)]="model" [ngbTypeahead]="search"/>
.TS file:
import {Component} from '#angular/core';
import {Observable} from 'rxjs';
import {debounceTime, distinctUntilChanged, map} from 'rxjs/operators';
const empId = ['643200','987900','043100','049400','189700','240800','617600','228700','188600','631200','695000','029500','626500','199700','619300','995300','123700','619400','230200','017300','237800'];
#Component({
selector: 'ngbd-typeahead-basic',
templateUrl: './typeahead-basic.html',
styles: [`.form-control { width: 300px; }`]
})
export class NgbdTypeaheadBasic {
public model: any;
search = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => term.length < 2 ? []
: empId.filter(v => v.toLowerCase().indexOf(term.toLowerCase()) > -1).slice(0, 10))
)
}
FYI, the JSON file is located in the same server at a location ../path/file.json in the structure:
[
{
"empId": "643200"
},
{
"empId": "987900"
},
{
"empId": "043100"
},
{
"empId": "049400"
},
{
"empId": "189700"
}
]
Again, PS: empId in the JSON file has over 10,000+ entries. This is just an example.

Resources