Rendering multiple graphs in react js from canvasjs - reactjs
I would like to render the stockprice graph referred from https://canvasjs.com/react-stockcharts/stockchart-date-time-axis-react/
import React, { Component } from 'react';
import CanvasJSReact from '../../assets/canvasjs.react';
var CanvasJSChart = CanvasJSReact.CanvasJSChart;
var CanvasJSStockChart = CanvasJSReact.CanvasJSStockChart;
class Chart extends Component {
render() {
const options = {
theme: "light2",
title:{
text:"React StockChart with Date-Time Axis"
},
subtitles: [{
text: "Price-Volume Trend"
}],
charts: [{
axisX: {
lineThickness: 5,
tickLength: 0,
labelFormatter: function(e) {
return "";
},
crosshair: {
enabled: true,
snapToDataPoint: true,
labelFormatter: function(e) {
return "";
}
}
},
axisY: {
title: "Litecoin Price",
prefix: "$",
tickLength: 0
},
toolTip: {
shared: true
},
data: [{
name: "Price (in USD)",
yValueFormatString: "$#,###.##",
type: "candlestick",
dataPoints : [
{ x: new Date("2016, 01, 01"), y: [36.040001, 37.500000, 35.790001, 36.950001] },
{ x: new Date("2016, 02, 01"), y: [37.099998, 39.720001, 37.060001, 39.169998] },
{ x: new Date("2016, 03, 01"), y: [38.669998, 39.360001, 37.730000, 38.820000] },
{ x: new Date("2016, 04, 01"), y: [38.869999, 39.669998, 37.770000, 39.150002] },
{ x: new Date("2016, 05, 01"), y: [39.099998, 43.419998, 38.580002, 43.209999] },
{ x: new Date("2016, 06, 01"), y: [43.209999, 43.889999, 41.700001, 43.290001] },
{ x: new Date("2016, 07, 01"), y: [43.250000, 43.500000, 40.549999, 40.880001] },
{ x: new Date("2016, 08, 01"), y: [40.849998, 41.700001, 39.549999, 40.610001] },
{ x: new Date("2016, 09, 01"), y: [40.619999, 41.040001, 36.270000, 36.790001] },
{ x: new Date("2016, 10, 01"), y: [36.970001, 39.669998, 36.099998, 38.630001] },
{ x: new Date("2016, 11, 01"), y: [38.630001, 42.840000, 38.160000, 40.380001] }
]
}]
},{
height: 100,
axisX: {
crosshair: {
enabled: true,
snapToDataPoint: true
}
},
axisY: {
title: "Volume",
prefix: "$",
tickLength: 0
},
toolTip: {
shared: true
},
data: [{
name: "Volume",
type: "column",
dataPoints : [
{ x: new Date("2016, 01, 01"), y: [100, 40] },
{ x: new Date("2016, 02, 01"), y: [50,60] },
{ x: new Date("2016, 03, 01"), y: [60, -50] },
{ x: new Date("2016, 04, 01"), y: [-50, 20] },
{ x: new Date("2016, 05, 01"), y: [20, -40] },
{ x: new Date("2016, 06, 01"), y: [43.209999, 43.290001] },
{ x: new Date("2016, 07, 01"), y: [43.250000, 40.880001] },
{ x: new Date("2016, 08, 01"), y: [40.849998, 40.610001] },
{ x: new Date("2016, 09, 01"), y: [40.619999, 36.790001] },
{ x: new Date("2016, 10, 01"), y: [36.970001, 38.630001] },
{ x: new Date("2016, 11, 01"), y: [38.630001, 40.380001] }
]
}]
}]
};
const containerProps = {
width: "100%",
height: "450px",
margin: "auto"
};
return (
<div>
<div>
{
// Reference: https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator
this.state.isLoaded &&
<CanvasJSStockChart containerProps={containerProps} options = {options}
/* onRef = {ref => this.chart = ref} */
/>
}
</div>
</div>
);
}
}
export default Chart;
Here is how I have tried. I found a single chart is rendered but more than one chart by using charts: [{}] isn't working. It shows nothing on my brower.
Has anybody had this kind of issue before? Do you think the reason is because I am using canvasjs graphs for free?
In case of column chart datapoints, y-value should be numeric but you are passing it as an array. If you like to show multiple values (a range) in y-value, you can use Range-Column Chart. Please find the working code below. Checkout this working sample.
import React, { Component } from 'react';
import CanvasJSReact from '../../assets/canvasjs.react';
var CanvasJSChart = CanvasJSReact.CanvasJSChart;
var CanvasJSStockChart = CanvasJSReact.CanvasJSStockChart;
class Chart extends Component {
render() {
const options = {
theme: "light2",
title:{
text:"React StockChart with Date-Time Axis"
},
subtitles: [{
text: "Price-Volume Trend"
}],
charts: [{
axisX: {
lineThickness: 5,
tickLength: 0,
labelFormatter: function(e) {
return "";
},
crosshair: {
enabled: true,
snapToDataPoint: true,
labelFormatter: function(e) {
return "";
}
}
},
axisY: {
title: "Litecoin Price",
prefix: "$",
tickLength: 0
},
toolTip: {
shared: true
},
data: [{
name: "Price (in USD)",
yValueFormatString: "$#,###.##",
type: "candlestick",
dataPoints : [
{ x: new Date("2016, 01, 01"), y: [36.040001, 37.500000, 35.790001, 36.950001] },
{ x: new Date("2016, 02, 01"), y: [37.099998, 39.720001, 37.060001, 39.169998] },
{ x: new Date("2016, 03, 01"), y: [38.669998, 39.360001, 37.730000, 38.820000] },
{ x: new Date("2016, 04, 01"), y: [38.869999, 39.669998, 37.770000, 39.150002] },
{ x: new Date("2016, 05, 01"), y: [39.099998, 43.419998, 38.580002, 43.209999] },
{ x: new Date("2016, 06, 01"), y: [43.209999, 43.889999, 41.700001, 43.290001] },
{ x: new Date("2016, 07, 01"), y: [43.250000, 43.500000, 40.549999, 40.880001] },
{ x: new Date("2016, 08, 01"), y: [40.849998, 41.700001, 39.549999, 40.610001] },
{ x: new Date("2016, 09, 01"), y: [40.619999, 41.040001, 36.270000, 36.790001] },
{ x: new Date("2016, 10, 01"), y: [36.970001, 39.669998, 36.099998, 38.630001] },
{ x: new Date("2016, 11, 01"), y: [38.630001, 42.840000, 38.160000, 40.380001] }
]
}]
},{
height: 100,
axisX: {
crosshair: {
enabled: true,
snapToDataPoint: true
}
},
axisY: {
title: "Volume",
prefix: "$",
tickLength: 0
},
toolTip: {
shared: true
},
data: [{
name: "Volume",
type: "rangeColumn",
dataPoints : [
{ x: new Date("2016, 01, 01"), y: [100, 40] },
{ x: new Date("2016, 02, 01"), y: [50,60] },
{ x: new Date("2016, 03, 01"), y: [60, -50] },
{ x: new Date("2016, 04, 01"), y: [-50, 20] },
{ x: new Date("2016, 05, 01"), y: [20, -40] },
{ x: new Date("2016, 06, 01"), y: [43.209999, 43.290001] },
{ x: new Date("2016, 07, 01"), y: [43.250000, 40.880001] },
{ x: new Date("2016, 08, 01"), y: [40.849998, 40.610001] },
{ x: new Date("2016, 09, 01"), y: [40.619999, 36.790001] },
{ x: new Date("2016, 10, 01"), y: [36.970001, 38.630001] },
{ x: new Date("2016, 11, 01"), y: [38.630001, 40.380001] }
]
}]
}]
};
const containerProps = {
width: "100%",
height: "450px",
margin: "auto"
};
return (
<div>
<div>
{
// Reference: https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logical--operator
this.state.isLoaded &&
<CanvasJSStockChart containerProps={containerProps} options = {options}
/* onRef = {ref => this.chart = ref} */
/>
}
</div>
</div>
);
}
}
export default Chart;
Related
Cannot find a way to correctly use chartjs annotation plugin with react
I am currently trying to use the plugin chartjs-plugin-annotation in my react project. Unfortunately, it is not working... He is my implementation : import React, { Component } from "react"; //import "./css/tideComponent.css"; import jsonData from "./ressources/tideOostende2023.json"; import "chart.js/auto"; import { Chart } from "react-chartjs-2"; import * as ChartAnnotation from "chartjs-plugin-annotation"; class Tide extends Component { state = { dayDate: new Date().toJSON().slice(5, 10), highTide: "", highTide2: "", lowTide: "", lowTide2: "", }; async componentDidMount() { const index = jsonData.findIndex( (item) => item.date === this.state.dayDate ); //TODO store tide in an array(using split method) & filter low to high to have a correct graph this.setState({ highTide: jsonData[index].highTide, highTide2: jsonData[index].highTide2, lowTide: jsonData[index].lowTide, lowTide2: jsonData[index].lowTide2, }); } timeToNumeric(tideTime) { const tideTimeSplitted = tideTime.split(":"); return tideTimeSplitted[0] * 1 + tideTimeSplitted[1] / 60; } handleTideData() { if ( this.timeToNumeric(this.state.highTide) < this.timeToNumeric(this.state.lowTide) ) return [ { x: -2, y: 0.5 }, { x: this.timeToNumeric(this.state.highTide), y: 1.5 }, { x: this.timeToNumeric(this.state.lowTide), y: 0.5 }, { x: this.timeToNumeric(this.state.highTide2), y: 1.5 }, { x: this.timeToNumeric(this.state.lowTide2), y: 0.5 }, { x: 26, y: 1.5 }, ]; return [ { x: -2, y: 1.5 }, { x: this.timeToNumeric(this.state.lowTide), y: 0.5 }, { x: this.timeToNumeric(this.state.highTide), y: 1.5 }, { x: this.timeToNumeric(this.state.lowTide2), y: 0.5 }, { x: this.timeToNumeric(this.state.highTide2), y: 1.5 }, { x: 26, y: 0.5 }, ]; } render() { const data = { datasets: [ { data: this.handleTideData(), fill: false, backgroundColor: "rgb(35, 71, 89, 0.88)", borderColor: " rgb(35, 71, 79, 0.88)", tension: 0.4, }, ], }; const options = { annotation: { annotations: [ { type: "line", mode: "horizontal", scaleID: "x", value: 1, borderColor: "white", borderWidth: 2, }, ], }, scales: { x: { min: 0, max: 24, ticks: { stepSize: 1 } }, y: { min: 0, max: 2.2, display: false }, }, showLine: true, pointStyle: false, plugins: { legend: { display: false }, }, }; return ( <div className="tideContainer"> <Chart type="scatter" data={data} options={options} plugins={ChartAnnotation} /> </div> ); } } export default Tide;` I tried different things but still not working. I also reviewed multiple question on SO but cannot find my solution. Chart Js is correctly working with my implementation, it is only the plugin that does not work. Thank you in advance for your great help !!!
I think plugins property in react-chartjs-2 should be an array, I guess. <Chart type="scatter" data={data} options={options} plugins={[ChartAnnotation]} /> The options config for annotation plugin is not in the right node. It must be added in options.plugins node. const options = { plugins: { // <-- to add, was missing annotation: { annotations: [ { type: "line", mode: "horizontal", scaleID: "x", value: 1, borderColor: "white", borderWidth: 2, }, ], }, }
Group and stacked bar chart in apex charts react js
I have created a horizontal bar stacked chart using apexcharts but I'm stuck at one and the last phase of completion and I tried so hard but were unable to find the solution, I've attached the actual chart and expected as well, all I want is just to group bars and show bars title. series and options: const series = [ { name: 'Order Date', data: [ { x: 'SWGR-MVA', y: ['2021-6-1', '2021-6-30'] }, { x: 'HV-8PT-1', y: ['2021-6-1', '2021-6-23'] }, { x: '8PT-1', y: ['2021-6-1', '2021-6-30'] }, { x: 'HV-8PT-2', y: ['2021-6-1', '2021-6-23'] }, { x: '8PT-2', y: ['2021-6-1', '2021-6-30'] }, { x: 'HV-45PT-1', y: ['2021-6-1', '2021-6-23'] }, { x: '45PT-1', y: ['2021-6-1', '2021-6-30'] }, { x: 'HV-45PT-2', y: ['2021-6-1', '2021-6-23'] }, { x: '45PT-2', y: ['2021-6-1', '2021-6-30'] }, { x: 'SWBD-AAA', y: ['2021-6-1', '2021-6-20'] }, { x: 'SWBD-BBB', y: ['2021-6-1', '2021-6-20'] }, { x: 'EM3VJOFPPF', y: ['2021-6-1', '2021-6-25'] }, { x: 'SWBD-CCC', y: ['2021-6-1', '2021-6-20'] }, { x: 'SWBD-DDD', y: ['2021-6-1', '2021-6-20'] }, { x: 'RA7PSYODY6', y: ['2021-6-1', '2021-6-25'] }, { x: 'ATS-8LSBB', y: ['2021-6-1', '2021-6-15'] }, { x: 'SWBD-8LSBB', y: ['2021-6-1', '2021-6-20'] }, { x: 'ATS-8XBB', y: ['2021-6-1', '2021-6-15'] }, { x: 'SWBD-8XBB', y: ['2021-6-1', '2021-6-20'] }, { x: 'ATS-45LSDD', y: ['2021-6-1', '2021-6-15'] }, { x: 'SWBD-45LSDD', y: ['2021-6-1', '2021-6-20'] }, { x: 'ATS-45XDD', y: ['2021-6-1', '2021-6-15'] }, { x: 'SWBD-45XDD', y: ['2021-6-1', '2021-6-20'] }, { x: 'SWBD-45XBB', y: ['2021-6-1', '2021-6-20'] }, ], }, { name: 'Order Date Delay', data: [{ x: 'SWGR-MVA', y: ['2021-6-20', '2021-6-30'] }], }, { name: 'Approval Drawings', data: [ { x: 'SWGR-MVA', y: ['2021-6-30', '2021-7-21'] }, { x: 'HV-8PT-1', y: ['2021-6-23', '2021-7-15'] }, { x: '8PT-1', y: ['2021-6-30', '2021-7-21'] }, { x: 'HV-8PT-2', y: ['2021-6-23', '2021-7-15'] }, { x: '8PT-2', y: ['2021-6-30', '2021-7-21'] }, { x: 'HV-45PT-1', y: ['2021-6-23', '2021-7-15'] }, { x: '45PT-1', y: ['2021-6-30', '2021-7-21'] }, { x: 'HV-45PT-2', y: ['2021-6-23', '2021-7-15'] }, { x: '45PT-2', y: ['2021-6-30', '2021-7-21'] }, { x: 'SWBD-AAA', y: ['2021-6-20', '2021-7-18'] }, { x: 'SWBD-BBB', y: ['2021-6-20', '2021-7-18'] }, { x: '7RV3BTUV2E', y: ['2021-6-25', '2021-7-18'] }, { x: 'SWBD-CCC', y: ['2021-6-20', '2021-7-18'] }, { x: 'SWBD-DDD', y: ['2021-6-20', '2021-7-18'] }, { x: 'C5B4JOLXM5', y: ['2021-6-25', '2021-7-18'] }, { x: 'ATS-8LSBB', y: ['2021-6-15', '2021-7-18'] }, { x: 'SWBD-8LSBB', y: ['2021-6-20', '2021-7-18'] }, { x: 'ATS-8XBB', y: ['2021-6-15', '2021-7-18'] }, { x: 'SWBD-8XBB', y: ['2021-6-20', '2021-7-18'] }, { x: 'ATS-45LSDD', y: ['2021-6-15', '2021-7-18'] }, { x: 'SWBD-45LSDD', y: ['2021-6-20', '2021-7-18'] }, { x: 'ATS-45XDD', y: ['2021-6-15', '2021-7-18'] }, { x: 'SWBD-45XDD', y: ['2021-6-20', '2021-7-18'] }, { x: 'SWBD-45XBB', y: ['2021-6-20', '2021-7-18'] }, ], }, { name: 'Approval Drawings Delay', data: [{ x: 'HV-8PT-1', y: ['2021-7-8', '2021-7-15'] }], }, { name: 'Release For Manufacture', data: [ { x: 'SWGR-MVA', y: ['2021-7-21', '2021-11-1'] }, { x: 'HV-8PT-1', y: ['2021-7-15', '2021-9-30'] }, { x: '8PT-1', y: ['2021-7-21', '2021-10-15'] }, { x: 'HV-8PT-2', y: ['2021-7-15', '2021-9-30'] }, { x: '8PT-2', y: ['2021-7-21', '2021-10-15'] }, { x: 'HV-45PT-1', y: ['2021-7-15', '2021-9-30'] }, { x: '45PT-1', y: ['2021-7-21', '2021-10-15'] }, { x: 'HV-45PT-2', y: ['2021-7-15', '2021-9-30'] }, { x: '45PT-2', y: ['2021-7-21', '2021-10-15'] }, { x: 'SWBD-AAA', y: ['2021-7-18', '2021-10-1'] }, { x: 'SWBD-BBB', y: ['2021-7-18', '2021-10-1'] }, { x: 'SWBD-CCC', y: ['2021-7-18', '2021-10-1'] }, { x: 'SWBD-DDD', y: ['2021-7-18', '2021-10-1'] }, { x: 'ATS-8LSBB', y: ['2021-7-18', '2021-8-31'] }, { x: 'SWBD-8LSBB', y: ['2021-7-18', '2021-10-1'] }, { x: 'ATS-8XBB', y: ['2021-7-18', '2021-8-31'] }, { x: 'SWBD-8XBB', y: ['2021-7-18', '2021-10-1'] }, { x: 'ATS-45LSDD', y: ['2021-7-18', '2021-8-31'] }, { x: 'SWBD-45LSDD', y: ['2021-7-18', '2021-10-1'] }, { x: 'ATS-45XDD', y: ['2021-7-18', '2021-8-31'] }, { x: 'SWBD-45XDD', y: ['2021-7-18', '2021-10-1'] }, { x: 'SWBD-45XBB', y: ['2021-7-18', '2021-10-1'] }, ], }, { name: 'Release For Manufacture Delay', data: [ { x: 'HV-45PT-1', y: ['2021-8-18', '2021-9-30'] }, { x: '45PT-1', y: ['2021-8-21', '2021-10-15'] }, ], }, { name: 'Test Date', data: [ { x: 'SWGR-MVA', y: ['2021-11-1', '2021-11-4'] }, { x: 'HV-8PT-1', y: ['2021-9-30', '2021-10-5'] }, { x: '8PT-1', y: ['2021-10-15', '2021-10-18'] }, { x: 'HV-8PT-2', y: ['2021-9-30', '2021-10-5'] }, { x: '8PT-2', y: ['2021-10-15', '2021-10-18'] }, { x: 'HV-45PT-1', y: ['2021-9-30', '2021-10-5'] }, { x: '45PT-1', y: ['2021-10-15', '2021-10-18'] }, { x: 'HV-45PT-2', y: ['2021-9-30', '2021-10-5'] }, { x: '45PT-2', y: ['2021-10-15', '2021-10-18'] }, { x: 'SWBD-AAA', y: ['2021-10-1', '2021-10-5'] }, { x: 'SWBD-BBB', y: ['2021-10-1', '2021-10-5'] }, { x: 'SWBD-CCC', y: ['2021-10-1', '2021-10-5'] }, { x: 'SWBD-DDD', y: ['2021-10-1', '2021-10-5'] }, { x: 'ATS-8LSBB', y: ['2021-8-31', '2021-9-4'] }, { x: 'SWBD-8LSBB', y: ['2021-10-1', '2021-10-5'] }, { x: 'ATS-8XBB', y: ['2021-8-31', '2021-9-4'] }, { x: 'SWBD-8XBB', y: ['2021-10-1', '2021-10-5'] }, { x: 'ATS-45LSDD', y: ['2021-8-31', '2021-9-4'] }, { x: 'SWBD-45LSDD', y: ['2021-10-1', '2021-10-5'] }, { x: 'ATS-45XDD', y: ['2021-8-31', '2021-9-4'] }, { x: 'SWBD-45XDD', y: ['2021-10-1', '2021-10-5'] }, { x: 'SWBD-45XBB', y: ['2021-10-1', '2021-10-5'] }, ], }, { name: 'Test Date Delay', data: [{ x: 'SWBD-8XBB', y: ['2021-10-2', '2021-10-5'] }], }, { name: 'Ship Date', data: [ { x: 'SWGR-MVA', y: ['2021-11-4', '2021-11-9'] }, { x: 'HV-8PT-1', y: ['2021-10-5', '2021-10-9'] }, { x: '8PT-1', y: ['2021-10-18', '2021-10-20'] }, { x: 'HV-8PT-2', y: ['2021-10-5', '2021-10-9'] }, { x: '8PT-2', y: ['2021-10-18', '2021-10-20'] }, { x: 'HV-45PT-1', y: ['2021-10-5', '2021-10-9'] }, { x: '45PT-1', y: ['2021-10-18', '2021-10-20'] }, { x: 'HV-45PT-2', y: ['2021-10-5', '2021-10-9'] }, { x: '45PT-2', y: ['2021-10-18', '2021-10-20'] }, { x: 'SWBD-AAA', y: ['2021-10-5', '2021-10-10'] }, { x: 'SWBD-BBB', y: ['2021-10-5', '2021-10-10'] }, { x: '2H30E81WAN', y: ['2021-10-5', '2021-10-10'] }, { x: 'SWBD-CCC', y: ['2021-10-5', '2021-10-10'] }, { x: 'SWBD-DDD', y: ['2021-10-5', '2021-10-10'] }, { x: '742INZHY93', y: ['2021-10-5', '2021-10-10'] }, { x: 'ATS-8LSBB', y: ['2021-9-4', '2021-9-8'] }, { x: 'SWBD-8LSBB', y: ['2021-10-5', '2021-10-10'] }, { x: 'ATS-8XBB', y: ['2021-9-4', '2021-9-8'] }, { x: 'SWBD-8XBB', y: ['2021-10-5', '2021-10-10'] }, { x: 'ATS-45LSDD', y: ['2021-9-4', '2021-9-8'] }, { x: 'SWBD-45LSDD', y: ['2021-10-5', '2021-10-10'] }, { x: 'ATS-45XDD', y: ['2021-9-4', '2021-9-8'] }, { x: 'SWBD-45XDD', y: ['2021-10-5', '2021-10-10'] }, { x: 'SWBD-45XBB', y: ['2021-10-5', '2021-10-10'] }, ], }, { name: 'Ship Date Delay', data: [ { x: 'SWBD-AAA', y: ['2021-10-1', '2021-10-10'] }, { x: 'SWBD-8LSBB', y: ['2021-10-2', '2021-10-10'] }, { x: 'ATS-8XBB', y: ['2021-9-3', '2021-9-8'] }, ], }, ]; const options = { plotOptions: { bar: { horizontal: true, barHeight: '63%', rangeBarGroupRows: true, }, }, dataLabels: { enabled: true, enabledOnSeries: [1, 3, 5, 7, 9], formatter: function (val) { var a = moment(val[0]); var b = moment(val[1]); var diff = b.diff(a, 'days'); return '+' + diff; //+ (diff > 1 ? ' days' : ' day') }, textAnchor: 'middle', distributed: false, offsetX: 0, offsetY: 0, style: { fontSize: '12px', colors: ['#ffffff'], }, dropShadow: { enabled: false, top: 1, left: 1, blur: 1, color: '#000', opacity: 0.45, }, }, colors: [ '#008FFB', '#0065b3', '#00E396', '#00b377', ], fill: { type: 'gradient', gradient: { shade: 'light', type: 'diagonal', shadeIntensity: 0.01, gradientToColors: undefined, inverseColors: false, opacityFrom: 0.7, opacityTo: 0.9, }, }, dropShadow: { enabled: true, top: 0, left: 0, blur: 2, opacity: 0.5, }, grid: { show: true, borderColor: '#DEE2E6', //'#90A4AE' strokeDashArray: 0, position: 'back', xaxis: { lines: { show: false, }, }, yaxis: { lines: { show: true, }, }, row: { colors: undefined, opacity: 0.1, }, column: { colors: undefined, opacity: 0.1, }, padding: { top: 0, right: 0, bottom: 0, left: 0, }, }, xaxis: { type: 'datetime', min: undefined, labels: { show: true, rotate: -45, rotateAlways: false, hideOverlappingLabels: true, showDuplicates: false, trim: false, minHeight: undefined, maxHeight: 120, style: { colors: [], fontSize: '12px', fontWeight: 400, cssClass: 'apexcharts-xaxis-label', }, offsetX: 0, offsetY: 0, format: undefined, formatter: undefined, datetimeUTC: true, datetimeFormatter: { year: 'yyyy', month: "MMM 'yy", day: 'dd MMM', hour: 'HH:mm', }, }, lines: { show: true, }, axisTicks: { show: true, borderType: 'solid', color: '#78909C', height: 6, offsetX: 0, offsetY: 0, }, axisBorder: { show: true, color: '#78909C', height: 1, width: '100%', offsetX: 0, offsetY: 0, }, crosshairs: { show: true, width: 1, position: 'back', opacity: 0.9, stroke: { color: '#b6b6b6', width: 0, dashArray: 0, }, fill: { type: 'solid', color: '#B1B9C4', gradient: { colorFrom: '#D8E3F0', colorTo: '#BED1E6', stops: [0, 100], opacityFrom: 0.4, opacityTo: 0.5, }, }, dropShadow: { enabled: false, top: 0, left: 0, blur: 1, opacity: 0.4, }, }, tooltip: { enabled: true, formatter: undefined, offsetY: 0, style: { fontSize: 0, fontFamily: 0, }, }, }, yaxis: { lines: { show: true, }, labels: { show: true, rotate: 0, rotateAlways: false, hideOverlappingLabels: true, showDuplicates: false, trim: false, minHeight: undefined, maxHeight: 120, style: { colors: [], fontSize: '12px', fontWeight: 400, cssClass: 'apexcharts-xaxis-label', }, offsetX: 0, offsetY: 0, format: undefined, formatter: undefined, datetimeUTC: true, datetimeFormatter: { year: 'yyyy', month: "MMM 'yy", day: 'dd MMM', hour: 'HH:mm', }, }, title: { text: 'Equipment Tags', rotate: -90, offsetX: -1, offsetY: 0, style: { color: undefined, fontSize: '12px', fontWeight: 600, cssClass: 'apexcharts-yaxis-title', }, }, axisTicks: { show: true, borderType: 'solid', color: '#78909C', height: 6, offsetX: 0, offsetY: 0, }, axisBorder: { show: true, color: '#78909C', height: 1, width: '100%', offsetX: 0, offsetY: 0, }, crosshairs: { show: true, width: 1, position: 'back', opacity: 0.9, stroke: { color: '#b6b6b6', width: 0, dashArray: 0, }, fill: { type: 'solid', color: '#B1B9C4', gradient: { colorFrom: '#D8E3F0', colorTo: '#BED1E6', stops: [0, 100], opacityFrom: 0.4, opacityTo: 0.5, }, }, dropShadow: { enabled: false, top: 0, left: 0, blur: 1, opacity: 0.4, }, }, }, annotations: { xaxis: [ { x: new Date('15 Sep 2021').getTime(), //new Date().getTime() borderColor: '#999', yAxisIndex: 0, label: { show: true, text: "Today's Date", style: { color: '#fff', background: '#775DD0', }, }, }, ], }, chart: { background: '#fff', foreColor: '#373d3f', }, legend: { show: true, showForSingleSeries: false, showForNullSeries: true, showForZeroSeries: true, position: 'right', horizontalAlign: 'left', floating: false, fontSize: '13px', fontWeight: 400, formatter: undefined, inverseOrder: false, width: undefined, height: undefined, tooltipHoverFormatter: undefined, customLegendItems: [], offsetX: 0, offsetY: 0, labels: { colors: undefined, useSeriesColors: false, }, markers: { width: 10, height: 10, strokeWidth: 0, strokeColor: '#fff', fillColors: undefined, radius: 12, customHTML: undefined, onClick: undefined, offsetX: 0, offsetY: 0, }, itemMargin: { horizontal: 20, vertical: 4, }, onItemClick: { toggleDataSeries: true, }, onItemHover: { highlightDataSeries: true, }, }, }; current output: What I want: Actual: ----------------------------- SWGR-MVA Original: ----------------------------- Expected output image just to get a clear idea of what I want:
chartjs doesn't render dates properly
I made a custom chartjs component in reactjs and want to render dates in xAxes and numbers from -1 to 1 in yAxes but it renders data not in a proper way. import React, { useRef, useEffect } from "react"; import Chart from "chart.js"; const ChartComponent = ({ data, label, min, max }) => { const canvasRef = useRef(null); useEffect(() => { const canvasObj = canvasRef.current; const context = canvasObj.getContext("2d"); new Chart(context, { type: "line", data: { datasets: [ { label: label, backgroundColor: "transparent", data: data, }, ], }, options: { scales: { xAxes: [ { type: "time", distribution: "linear", time: { unit: "month", displayFormats: { quarter: "YYYY mm dd", }, }, }, ], yAxes: [ { ticks: { suggestedMax: max, suggestedMin: min, }, }, ], }, }, }); }, [data, label, min, max]); return <canvas ref={canvasRef}> </canvas>; }; export default ChartComponent; and I'm passing the data like this to the component <ChartComponent min={-1} max={1} label="date" data={[ { x: "30/03/2018", y: 0.1158, }, { x: "24/09/2018", y: 0.1975, }, { x: "23/12/2018", y: 0.1913, }, { x: "23/03/2019", y: 0.2137, }, ]} />; I have to metion that I have done the same thing and that is working alright this is example below <ChartComponent min={1270} max={1272} label=" مساحت دریاچه" data={[ { x: "04/02/2017", y: 1270.7, }, { x: "06/26/2017", y: 1270.74, }, { x: "09/19/2017", y: 1270.31, }, { x: "12/18/2017", y: 1270.28, }, { x: "06/16/2018", y: 1270.81, }, { x: "09/24/2018", y: 1270.27, }, { x: "12/23/2018", y: 1270.54, }, { x: "05/25/2019", y: 1271.94, }, { x: "06/18/2019", y: 1271.84, }, { x: "09/19/2019", y: 1271.31, }, { x: "12/18/2019", y: 1271.25, }, { x: "03/12/2020", y: 1271.48, }, { x: "06/25/2020", y: 1271.72, }, ]} />; any recommendation would be appreciated. thank you.
The problem is that the date strings you provide are not of ISO 08601 nor RFC 2822 Date time format, hence they cannot be parsed by Moment.js, which is internally used by Chart.js. To make it work, you have to define xAxes.time.parser: "DD.MM.YYYY". Please take a look at below runnable code and see how it works. This is pure JavaScript example but it should also work with react-chartjs-2. new Chart("myChart", { type: "line", data: { datasets: [{ label: 'My Dataset', data: [ { x: "30/03/2018", y: 0.1158 }, { x: "24/09/2018", y: -0.1975 }, { x: "23/12/2018", y: 0.1913 }, { x: "23/03/2019", y: -0.2137 } ], fill: false }] }, options: { scales: { xAxes: [{ type: "time", time: { parser: "DD.MM.YYYY", unit: "month", displayFormats: { month: "YYYY MM DD", } } }], yAxes: [{ ticks: { suggestedMax: 1, suggestedMin: -1 } }] } } }); <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.bundle.min.js"></script> <canvas id="myChart" height="100"></canvas> As I already mentioned, Chart.js internally uses Moment.js for the functionality of the time axis. Therefore make sure to use the bundled version of Chart.js that includes Moment.js in a single file.
React-Vega - WARN Infinite extent for field "x": [Infinity, -Infinity]
The code work perfectly in the vega online editor. But there are warnings in the console while using in react component and the X-axis is not rendering in the output. import React from 'react'; import { Vega } from 'react-vega'; import { VisualizationSpec } from 'vega-embed'; export function LineGraph() { const specs: VisualizationSpec = { $schema: 'https://vega.github.io/schema/vega/v5.json', description: 'A basic line chart example.', width: 500, height: 200, padding: 5, signals: [], data: [ { name: 'table', format: { parse: { x: 'date' }, }, }, ], scales: [ { name: 'x', type: 'time', range: 'width', domain: { data: 'table', field: 'x' }, }, { name: 'y', type: 'linear', range: 'height', nice: true, zero: true, domain: { data: 'table', field: 'y' }, }, ], axes: [ { orient: 'bottom', scale: 'x' }, { orient: 'left', scale: 'y' }, ], marks: [ { type: 'line', from: { data: 'table' }, encode: { enter: { x: { scale: 'x', field: 'x' }, y: { scale: 'y', field: 'y' }, stroke: { value: 'red' }, strokeWidth: { value: 2 }, }, }, }, ], }; const data: any = { table: [ { x: '01-08-2020', y: 28, c: 0 }, { x: '01-03-2020', y: 43, c: 0 }, { x: '01-01-2020', y: 81, c: 0 }, { x: '01-09-2020', y: 19, c: 0 }, { x: '01-02-2020', y: 52, c: 0 }, { x: '01-04-2020', y: 24, c: 0 }, { x: '01-07-2020', y: 87, c: 0 }, { x: '01-07-2020', y: 17, c: 0 }, { x: '01-08-2020', y: 68, c: 0 }, { x: '01-09-2020', y: 49, c: 0 }, ], }; const signalListeners = {}; return ( <div> <Vega data={data} signalListeners={signalListeners} spec={specs} /> </div> ); } Warnings: WARN Infinite extent for field "y": [Infinity, -Infinity] WARN Infinite extent for field "x": [Infinity, -Infinity] How to define the extent in vega?
There are two parts to this error - the console warning, and the lack of rendering. The console warning gets thrown in digest cycles when the data hasn't yet been injected into the spec; not ideal, but AFAIK can be ignored. The rendering looks to be due to an error in how react-vega handles the date parsing. Instead of passing in date as strings, first convert them to Date objects, then pass in the modified data to the Vega component.
angularjs nvd3 multichart with line and scatterplot not working
I'm using multichart with line and scatterplot. The line chart works fine but the scatterplot data does not plot correctly for the x-axis data. Can someone please provide a working example other than the one on github? Here is the selections: $scope.options = { chart: { type: 'multiChart', height: 450, margin: { top: 30, right: 60, bottom: 50, left: 70 }, color: d3.scale.category10().range(), //useInteractiveGuideline: true, duration: 500, xAxis: { ticks: 10, tickFormat: function (d) { return d3.format(',10d')(d); } }, yAxis1: { ticks: 10, tickFormat: function (d) { return d3.format('10d')(d); } } } }; function generateData() { var data1 = [{ x: 0, y: 25 }, { x: 25, y: 25 }, { x: 25, y: 0 }]; var data2 = [{ x: 0, y: 50 }, { x: 50, y: 50 }, { x: 50, y: 0 }]; var data3 = [{ x: 0, y: 75 }, { x: 75, y: 75 }, { x: 75, y: 0 }]; var data4 = [{ x: 0, y: 100 }, { x: 100, y: 100 }, { x: 100, y: 0 }]; var scatter = [{ x: 10, y: 30, size: Math.random(), shape: 'circle' }, { x: 20, y: 50, size: Math.random(), shape: 'circle' }, { x: 30, y: 80, size: Math.random(), shape: 'circle' }]; var testdata = []; testdata.push({ key: 'Stream1', values: data1 }); testdata.push({ key: 'Stream2', values: data2 }); testdata.push({ key: 'Stream3', values: data3 }); testdata.push({ key: 'Stream4', values: data4 }); testdata.push({ key: 'Stream5', values: scatter }); testdata[0].type = 'line'; testdata[0].yAxis = 1; testdata[1].type = 'line'; testdata[1].yAxis = 1; testdata[2].type = 'line'; testdata[2].yAxis = 1; testdata[3].type = 'line'; testdata[3].yAxis = 1; testdata[4].type = 'scatter'; testdata[4].yAxis = 1; return testdata; }
I also encounter this problem, and my workaround is to add a dummy scatter series with the start and end point only, and the series color is transparent. For example: line series: 1,2,3,4,5...100 scatter 1: 5, 67, 83 dummy scatter: 1, 100 Then the x-domain of the scatter will scale with the line series