Graph not returning after OnClick - reactjs

I am trying to plot this graph on react. My graph is not displaying after OnClick. Can anyone let me know my mistake.
Or if there is any other way or method to display graphs on React, please let me know.
I have defined a function, and called it in OnClick. I have also imported all the necessary libraries. I don't see where I am going wrong. I don't get any errors.
Any help would be appreciated.
import React from "react";
import Plot from 'react-plotly.js'
import Heading from 'react-plotly.js'
import LineSeries from 'react-plotly.js'
import Axis from 'react-plotly.js'
import factor from 'react-plotly.js'
import Legend from 'react-plotly.js'
function Example () {
<Plot
width={550}
height={500}
margin={{ bottom: 50, left: 90, top: 50, right: 100 }}
>
<Heading
title="Electrical characterization"
subtitle="current vs voltage"
/>
<LineSeries
data={[
{ x: 0, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 3 },
{ x: 4, y: 3 },
{ x: 5, y: 3 },
]}
xAxis="x"
yAxis="y"
lineStyle={{ strokeWidth: 3 }}
label="Vg = 7V"
displayMarker={false}
/>
<LineSeries
data={[
{ x: 0, y: 0 },
{ x: 1, y: 1 },
{ x: 2, y: 2 },
{ x: 3, y: 3 },
{ x: 4, y: 3 },
{ x: 5, y: 3 },
]}
xAxis="x"
yAxis="y"
displayMarker={true}
markerShape="circle"
label="Vg = 3V"
/>
<Axis
id="x"
position="bottom"
label="Drain voltage [V]"
displayPrimaryGridLines
max={6.1 / factor}
/>
<Axis
id="y"
position="left"
label="Drain current [mA]"
displayPrimaryGridLines
max={6.1 * factor}
/>
<Legend position="right" />
</Plot>
return (
<div className="input-group-append">
<button className="btn btn-sm btn-primary" onClick={Example}>Graph</button>
</div>
)
}
export default Example;

Related

react-grid-layout cannot reset to original preconfigured layout after dragging elements

Using react-grid-layout
With the following code if I
click on "set layouts 1" (or 2) then
Drag an item
click on the SAME "set layouts 1" (or 2) the grid layout does not reset to the original.
If after dragging the elements and I click the other layout button and then the original one then both do work again.
Why is this, and how do I force it to reset the layout directly (without intermediate step of setting another layout)
import React, { useState } from "react";
import { Responsive, WidthProvider } from "react-grid-layout";
import "/node_modules/normalize.css/normalize.css";
import "/node_modules/#blueprintjs/icons/lib/css/blueprint-icons.css";
import "/node_modules/#blueprintjs/core/lib/css/blueprint.css";
const ResponsiveReactGridLayout = WidthProvider(Responsive);
export function App(): any {
console.log("app called");
var layout1 = [
{ w: 1, h: 1, x: 0, y: 1, i: "1", moved: false, static: false },
{ w: 1, h: 1, x: 1, y: 2, i: "2", moved: false, static: false },
{ w: 1, h: 1, x: 2, y: 3, i: "3", moved: false, static: false },
{ w: 1, h: 1, x: 3, y: 4, i: "4", moved: false, static: false }
];
var layout2 = [
{ w: 1, h: 1, x: 2, y: 1, i: "1", moved: false, static: false },
{ w: 1, h: 1, x: 3, y: 3, i: "2", moved: false, static: false },
{ w: 1, h: 1, x: 4, y: 4, i: "3", moved: false, static: false },
{ w: 1, h: 1, x: 5, y: 5, i: "4", moved: false, static: false }
];
const presetLayouts1 = {
lg: layout1,
md: layout1,
sm: layout1,
xs: layout1,
xxs: layout1
};
const presetLayouts2 = {
lg: layout2,
md: layout2,
sm: layout2,
xs: layout2,
xxs: layout2
};
const [layouts, setLayouts] = useState(presetLayouts1);
var gridResponse = (
<div key="main1">
<button
key="5233FC6D-B38A-430A-930C-99978922950C"
onClick={() => setLayouts(presetLayouts2)}
>
Set Layouts 2
</button>
<button
key="6233FC6D-B38A-430A-930C-99978922950C"
onClick={() => setLayouts(presetLayouts1)}
>
Set Layouts 1
</button>
<ResponsiveReactGridLayout
cols={{ lg: 12, md: 10, sm: 6, xs: 6, xxs: 6 }}
compactType="vertical"
layouts={layouts}
isDraggable={true}
isResizable={true}
//rowHeight={15}
measureBeforeMount={false}
autoSize={true}
breakpoints={{ lg: 1200, md: 1000, sm: 800, xs: 600, xxs: 400 }}
>
<div key="1" style={{ padding: "15px" }}>
Div content 1
</div>
<div key="2" style={{ padding: "15px" }}>
Div content 2
</div>
<div key="3" style={{ padding: "15px" }}>
Div content 3
</div>
<div key="4" style={{ padding: "15px" }}>
Div content 4
</div>
</ResponsiveReactGridLayout>
</div>
);
return [gridResponse];
}
export default App;
Try this
Callback so you can save the layout.
Calls back with (currentLayout) after every drag or resize stop.
onLayoutChange: (layout: Layout) => void,

React vis Line Chart not rendering properly

I am using react-vis library for one of my projects. I copied the source code of a sample chart from one of their examples on the site. I copied the example directly but still the chart does not look as expected.
import React from 'react';
import {curveCatmullRom} from 'd3-shape';
import {
XYPlot,
XAxis,
YAxis,
HorizontalGridLines,
VerticalGridLines,
LineSeries
} from 'index';
export default function Example(props) {
return (
<XYPlot width={300} height={300}>
<HorizontalGridLines style={{stroke: '#B7E9ED'}} />
<VerticalGridLines style={{stroke: '#B7E9ED'}} />
<XAxis
title="X Axis"
style={{
line: {stroke: '#ADDDE1'},
ticks: {stroke: '#ADDDE1'},
text: {stroke: 'none', fill: '#6b6b76', fontWeight: 600}
}}
/>
<YAxis title="Y Axis" />
<LineSeries
className="first-series"
data={[{x: 1, y: 3}, {x: 2, y: 5}, {x: 3, y: 15}, {x: 4, y: 12}]}
style={{
strokeLinejoin: 'round',
strokeWidth: 4
}}
/>
<LineSeries className="second-series" data={null} />
<LineSeries
className="third-series"
curve={'curveMonotoneX'}
data={[{x: 1, y: 10}, {x: 2, y: 4}, {x: 3, y: 2}, {x: 4, y: 15}]}
strokeDasharray="7, 3"
/>
<LineSeries
className="fourth-series"
curve={curveCatmullRom.alpha(0.5)}
data={[{x: 1, y: 7}, {x: 2, y: 11}, {x: 3, y: 9}, {x: 4, y: 2}]}
/>
</XYPlot>
);
}
For this issue, you can import style.css from react-vis in your component like below,
import "react-vis/dist/style.css";
Working Code :- https://codesandbox.io/s/musing-yonath-bgg1i?file=/src/App.js

Recharts how to display an additional diagonal line with different data set to a line chart

What I am trying to do is the following:
But I can't manage to get it working. I was able to get the data to read in from 2 different data sources but it somehow does not render the second line properly on the x-axis.
Here is the example code:
import React from "react";
import { render } from "react-dom";
import { LineChart, Line, XAxis, YAxis, ReferenceLine } from "recharts";
const styles = {
fontFamily: "sans-serif",
textAlign: "center"
};
const data = [];
const maxBudget = 300;
for (let i = 0; i < 20; i++) {
let d = {
day: i,
value: Math.random() * (maxBudget + 50) + 100
};
data.push(d);
}
const testline = [{ x: 0, y: 300 }, { x: 20, y: 0 }]
const App = () => (
<div style={styles}>
<LineChart
width={500}
height={300}
margin={{ top: 5, right: 20, bottom: 5, left: 0 }}
>
<Line type="monotone" data={data} dataKey="value" stroke="#8884d8" dot={false} />
<Line type="linear" data={testline} dataKey="y" stroke="#FF3333" dot={false} strokeWidth={2} />
<XAxis dataKey="day" type="number" tickCount={11} />
<YAxis />
<ReferenceLine
y={maxBudget}
label={{
position: "center",
value: "Max budget"
}}
strokeDasharray="5 5"
/>
</LineChart>
</div>
);
render(<App />, document.getElementById("root"));
ok I m stupid. I just found it out by myself. Sometimes you don't see the wood for the trees for days :(.
The x value must be the same as defined in the XAxis so I had to change x:0 to day:0:
const testline = [{ day: 0, y: 300 }, { day: 20, y: 0 }]
Now it works

Resize High Charts based on grid layout width and height using react

I have a wrote a grid layout component in react. Inside the grid, I have loaded the high chart and it works great. On resizing the width and height of the grid, the high chart doesn't resize and it breaks when resizing.
I have searched for stack overflow and found the same question in here "Resize highcharts using react-grid-layout not working". Then i came to know that chart reflow method is to be called to make the high chart fit with in the grid when the grid layout changes. Since i am new to react, I don't know how to make it possible. Help me with some solutions.
Here's the code i tried:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import '/home/paulsteven/Documents/resize-ui/resize_ui/node_modules/react-grid-layout/css/styles.css';
import '/home/paulsteven/Documents/resize-ui/resize_ui/node_modules/react-resizable/css/styles.css';
import GridLayout from 'react-grid-layout';
import BarChart from "highcharts-react-official";
class MyFirstGrid extends React.Component {
state ={
options :{reflow:true,
title: {
text: 'Fruit Consumption'
},
subtitle: {
text: 'Steven Chart'
},
chart: {
type: 'bar'
},
xAxis: {
categories: ['Apples', 'Bananas', 'Oranges']
},
yAxis: {
title: {
text: 'Fruit eaten'
}
},
series: [{
name: 'Prince',
data: [4, 4, 2]
}, {
name: 'Syed',
data: [5, 3, 5]
},
{
name: 'Sam',
data: [1, 3, 3]
}]
}
}
render() {
// layout is an array of objects, see the demo for more complete usage
var layout = [
{i: 'a', x: 0, y: 0, w: 5, h: 2},
{i: 'b', x: 1, y: 0, w: 3, h: 2},
{i: 'c', x: 4, y: 0, w: 1, h: 2}
];
return (
<GridLayout className="layout" layout={layout} cols={12} rowHeight={30} width={1200}>
<div style={{backgroundColor: 'grey'}} key="a">
{/* <div id="container" style={{width:'100%',height:'400px'}}></div> */}
<BarChart options ={this.state.options} />
</div>
<div style={{backgroundColor: 'red'}}key="b">b</div>
<div style={{backgroundColor: 'blue'}} key="c">c</div>
</GridLayout>
)
}
}
export default MyFirstGrid;
I think the reason why the functionality of resizing highchrts is not working you can find it from the documentation of 'reflow' from hightcharts
By default, the chart reflows automatically to its container following a window.resize event, as per the chart.reflow option. However, there are no reliable events for div resize, so if the container is resized without a window resize event, this must be called explicitly.
The situation matches what you did now: you tried to resize the div itself but not the window size, so it doesn't work as you expected.
What I did to make my charts resizable in grid as following:
Create a function to do reflow
const onLayoutChange = () => {
for (let i = 0; i < Highcharts.charts.length; i += 1) {
if (Highcharts.charts[i] !== undefined) {
Highcharts.charts[i].reflow(); // here is the magic to update charts' looking
}
}
};
Use onLayoutChange provided by react-grid-layout
<GridLayout
cols={12}
layout={layout}
rowHeight={30}
width={1200}
onLayoutChange={() => onLayoutChange()}
>
...
</GridLayout>
And then...BANG! you got resizable charts controlled by the resize button in react-grid-layout.
Make it easy to understand, you can view live working here:
https://codesandbox.io/s/resize-highcharts-in-grid-9e625?file=/src/App.js
Here's the Solution to make it fit in the grid layout:
import React from 'react';
import './App.css';
import '/node_modules/react-grid-layout/css/styles.css';
import '/node_modules/react-resizable/css/styles.css';
import GridLayout from 'react-grid-layout';
import Highcharts from "highcharts/highstock";
import HighchartsReact from "highcharts-react-official";
const options = {
series: [
{
data: [1, 2, 3]
}
]
};
class MyFirstGrid extends React.Component {
constructor(props) {
super(props);
this.myRef = React.createRef();
this.conRef = React.createRef();
}
render() {
// layout is an array of objects, see the demo for more complete usage
var layout = [
{ i: "a", x: 0, y: 0, w: 5, h: 5 },
{ i: "b", x: 1, y: 0, w: 3, h: 2 },
{ i: "c", x: 4, y: 0, w: 1, h: 2 }
];
return (
<GridLayout
className="layout"
layout={layout}
cols={12}
rowHeight={30}
width={1200}
onResizeStop={function(event) {
this.myRef.current.chart.setSize(this.conRef.current.clientWidth,this.conRef.current.clientHeight)
console.log('hello', event);
}.bind(this)}
>
<div ref={this.conRef} style={{ backgroundColor: "#00000000" }} key="a">
<HighchartsReact
ref= {this.myRef}
containerProps={{ style: { width: '100%', height: '100%' } }}
options={options}
highcharts={Highcharts}
/>
</div>
<div style={{ backgroundColor: "red" }} key="b">
b
</div>
<div style={{ backgroundColor: "blue" }} key="c">
c
</div>
</GridLayout>
);
}
}
export default MyFirstGrid;

show date in( MM-DD) format in x-axis using react-vis

I am trying to implement react-vis in my project. I need to show data based on date. I am using tickFormat for it but it is showing same date two times in x-axis.I am adding few lines of my code here.
<XYPlot
width={1140}
height={440}
>
<LineMarkSeries
lineStyle={{stroke: '#e0e0e0'}}
markStyle={{stroke: '#6dc6c1'}}
style={{
strokeWidth: '2px'
}}
strokeStyle="solid"
data={[
{
x: Date.parse("05/05/2019"),
y: 0
},
{
x: Date.parse("05/12/2019"),
y: 12
},
{
x: Date.parse("05/13/2019"),
y: 16
}
]}
/>
<XAxis
attr="x"
attrAxis="y"
orientation="bottom"
tickFormat={function tickFormat(d){return
moment(d).format("MMM DD")}}
tickLabelAngle={0}
/>
<YAxis />
</XYPlot>
If you specify xType as ordinal, it will use your x value as the tick label (eg: like a bar chart). Thus, you don't have to have a redundant conversion of your x values using Date.parse.
<XYPlot
width={1140}
height={440}
xType='ordinal'
>
<LineMarkSeries
lineStyle={{stroke: '#e0e0e0'}}
markStyle={{stroke: '#6dc6c1'}}
style={{ strokeWidth: '2px' }}
strokeStyle="solid"
data={[
{
x: "05/05/2019",
y: 0
},
{
x: "05/12/2019",
y: 12
},
{
x: "05/13/2019",
y: 16
}
]}
/>
<XAxis />
</XYPlot>

Resources