Consuming React d3 area chart using react-d3-components - reactjs

I am creating a weather application where I am consuming OpenWeatherMap to get forecast data. I want to plot the data similar to google weather widget in an area chart.
I could not find much documentation for D#, I found react-d3-componetns npm package.
Documentaton shows to use ReactDOM.render
Below way of consuming is not working
import React from "react";
import ReactDom from 'react-dom';
import "./styles.css";
import ReactD3 from "react-d3-components";
export class WeatherGraph extends React.Component {
constructor(props) {
super(props);
this.state = {
data: [
{
label: "somethingA",
values: [
{ x: 0, y: 2 },
{ x: 1.3, y: 5 },
{ x: 3, y: 6 },
{ x: 3.5, y: 6.5 },
{ x: 4, y: 6 },
{ x: 4.5, y: 6 },
{ x: 5, y: 7 },
{ x: 5.5, y: 8 }
]
}
]
};
}
ReactDom.render(
<ReactD3.AreaChart
data={this.state.data}
width={400}
height={400}
yOrientation='right' // if you do not provide right default left orientation for yAxis will be used
margin={{top: 10, bottom: 50, left: 50, right: 10}}/>,
document.getElementById('location')
)
}
Below is the codesandbox link
https://codesandbox.io/s/weather-report-rgu7y-regtl
Could you suggest the right way of consuming it or refer to any examples ?

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,
},
],
},
}

React Js Plotly Remove Time Gaps In Graph

I am trying to plot stock data, but there are gaps in the graph where data for these time periods don't exist. How do I remove these gaps? I found that there is an update_xaxes function but I'm not sure how to apply it to my case.
My code:
import React, { useState, Component, useEffect } from "react";
import Plot from 'react-plotly.js';
export const Chart = () => {
return (
<div>
<Plot
data={[
{
x: data['dates'],
y: data['open_price'],
type: 'scatter'
}
]}
layout={{
autosize: false,
width: 1500,
height: 800,
xaxis: {
color: 'red',
rangebreaks: {
bounds: ["sat", "mon"],
values: ["2015-12-25", "2016-01-01"],
bounds: [17, 9], pattern: "hour"
}
}
}}
/>
</div>
)
}
react-ploty allows configuring the underlying ploty configuration through props. The data props you're already using accepts the same Traces Object than regular ploty. Therefore you can use rangebreaks as suggested by #r-beginners to achieve the desired effect.
xaxis: {
color: 'red',
rangebreaks: [
{ bounds: ["sat", "mon"] },
{
values: ["2019-12-25"]
},
{ bounds: [17, 9], pattern: "hour" }
]
}
I fixed it by making rangebreaks an array.

Adding imports from "#rainbow-me/animated-charts" crashes Expo app: "TypeError: this.InnerNativeModule.configureProps is not a function."

I am using a Windows computer and working on a React Native project using Yarn and Expo.
I am trying to use "#rainbow-me/animated-charts".
Here are the summarized steps I took to install their package, as per their documentation.
Installed react-native-reanimated (~2.3.1).
Adjusted my babel.config.js file.
Installed #rainbow-me/animated-charts (^1.0.0-alpha.6)
Cleared Expo bundler cache using "expo start --clear"
I created a separate Screen to replicate this issue using the example code provided in their Github repo:https://github.com/rainbow-me/react-native-animated-charts
import React from "react";
import { View, Text, Dimensions } from "react-native";
import {
ChartDot,
ChartPath,
ChartPathProvider,
monotoneCubicInterpolation,
} from "#rainbow-me/animated-charts";
export const { width: SIZE } = Dimensions.get("window");
const data = [
{ x: 1453075200, y: 1.47 },
{ x: 1453161600, y: 1.37 },
{ x: 1453248000, y: 1.53 },
{ x: 1453334400, y: 1.54 },
{ x: 1453420800, y: 1.52 },
{ x: 1453507200, y: 2.03 },
{ x: 1453593600, y: 2.1 },
{ x: 1453680000, y: 2.5 },
{ x: 1453766400, y: 2.3 },
{ x: 1453852800, y: 2.42 },
{ x: 1453939200, y: 2.55 },
{ x: 1454025600, y: 2.41 },
{ x: 1454112000, y: 2.43 },
{ x: 1454198400, y: 2.2 },
];
// const points = monotoneCubicInterpolation({ data, range: 40 });
const TestingScreen = () => {
return (
<View style={{ backgroundColor: "black" }}>
<ChartPathProvider data={{ points, smoothingStrategy: "bezier" }}>
<ChartPath height={SIZE / 2} stroke="yellow" width={SIZE} />
<ChartDot style={{ backgroundColor: "blue" }} />
</ChartPathProvider>
</View>
);
};
export default TestingScreen;
Here is the issue
As soon as I add the imports line:
import {
ChartDot,
ChartPath,
ChartPathProvider,
monotoneCubicInterpolation,
} from "#rainbow-me/animated-charts";
Expo crashes, see below:
Is there a solution to this?

Error in rendering a React highcharts x-range chart

I am trying to re-create the highcharts x-range demo chart (https://www.highcharts.com/demo/x-range) using the official React wrapper and I'm getting a "ReferenceError: Highcharts is not defined" error. I'm new to both react and highcharts so not sure what I'm missing here
here's the code:
import React, { useState } from 'react';
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
const AccountContractHistory = (props) => {
const [chartOptions, setChartOptions] = useState({
chart: {
type: 'xrange'
},
title: {
text: 'Highcharts X-range'
},
xAxis: {
type: 'datetime'
},
yAxis: {
title: {
text: ''
},
categories: ['Prototyping', 'Development', 'Testing'],
reversed: true
},
series: [{
name: 'Project 1',
// pointPadding: 0,
// groupPadding: 0,
borderColor: 'gray',
pointWidth: 20,
data: [{
x: Date.UTC(2014, 10, 21),
x2: Date.UTC(2014, 11, 2),
y: 0,
partialFill: 0.25
}, {
x: Date.UTC(2014, 11, 2),
x2: Date.UTC(2014, 11, 5),
y: 1
}, {
x: Date.UTC(2014, 11, 8),
x2: Date.UTC(2014, 11, 9),
y: 2
}, {
x: Date.UTC(2014, 11, 9),
x2: Date.UTC(2014, 11, 19),
y: 1
}, {
x: Date.UTC(2014, 11, 10),
x2: Date.UTC(2014, 11, 23),
y: 2
}],
dataLabels: {
enabled: true
}
}],
});
return (
<div>
<HighchartsReact
highcharts={Highcharts}
options={chartOptions}
/>
</div>
)
};
export default AccountContractHistory;
Would appreciate any help in figuring out what I'm doing wrong
Thanks
You can find a demo of this chart in React here:
https://stackblitz.com/edit/react-highcharts-xrange-demo
You have to remember to properly load highcharts modules:
https://github.com/highcharts/highcharts-react#how-to-add-a-module
Seeing same error but (using Typescript) for my fix i needed to pass in Highcharts to the xrange like so:
import Highcharts from 'highcharts';
import HighchartsReact from 'highcharts-react-official';
import xrange from 'highcharts/modules/xrange';
xrange(Highcharts);
and then the markup:
<HighchartsReact
highcharts={Highcharts}
options={options}
{...this.props}
/>

D3 - adding an interpolated point to a trend line

I am writing a React application and want to draw a line chart with an interpolated point, similar to the blue cross in the below:
I have been able to achieve mostly what I want using Nivo but this only drawers points based explicitly on data. What I want to do is illustrate an interpolated point along the trend line based on the x-axis. Is there a way to do this, either using Nivo or D3 directly?
The code I'm using for my line graph currently:
import React, { Component } from 'react';
import { Line } from 'nivo';
class LineGraph extends Component {
render() {
return (
<div className="line-graph">
<Line
width={600}
height={350}
margin={{
top: 20,
right: 20,
bottom: 60,
left: 60,
}}
data={[{
id: 'Distribution',
data: [
{x: '1', y: 0.09 },
{x: '2', y: 0.16 },
{x: '3', y: 0.5 },
{x: '4', y: 0.06 },
{x: '5', y: 0.19 },
]
}]}
colors="#ff6384"
enableArea
yScale={{ type: 'linear', stacked: false }}
curve="monotoneX"
dotSize={8}
dotBorderColor="#ff6384"
dotBorderWidth={2}
/>
</div>
);
}
}

Resources