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}
/>
Related
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,
},
],
},
}
I am using react-plotly.js Bar-Chart for which I want to show a Tooltip when we hover over a particular Y-axis label that will include a delete button with a data summary.
I have used a custom tooltip first to apply on the label of y-axis but I am not able to see any change on it.
I have also tried a normal HTML tooltip but still it was not visible for the react-plotly.js label.
import { HoverCard, Tooltip } from 'office-ui-fabric-react'
import React, { useState } from 'react'
import Plot from '../Plot'
export default function IndividualView() {
const customTag = '<custom-tag>Hello World</custom-tag>'
const trace1 = {
y: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
x: [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996,
7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998,
],
name: 'SF Zoo',
type: 'bar',
orientation: 'h',
}
const trace2 = {
y: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
x: [1.5, 2.5, 5, 6, 7, 8, 15.5, 18],
name: 'LA Zoo',
type: 'bar',
orientation: 'h',
}
const style = {
width: 950,
height: 500,
// border: '1px solid black',
margin_left: 10,
}
const data = [trace1, trace2]
const layout = {
yaxis: {
categories: ['Japan', 'United Kingdom', 'Canada', 'Netherlands', 'United States', 'Belgium', 'Sweden', 'Switzerland'],
tickInterval: 1,
labels: {
enabled: true,
formatter() {
return `<span title="My custom title">${this.value}</span>`
},
useHTML: true,
style: {
color: 'white',
},
},
},
annotations: [
{
xref: 'paper',
yref: 'paper',
x: -2,
y: -0.109,
showarrow: false,
font: {
family: 'Arial',
size: 1,
color: 'black',
},
},
],
dangerouslySetInnerHTML: { __html: customTag },
}
for (let i = 0; i < data[0].y.length; i++) {
const result = {
xref: 'x',
yref: 'y',
x: 30,
y: data[0].x[i],
text: `${data[0].x[i]}%`,
font: {
family: 'Arial',
size: 12,
color: 'green',
},
showarrow: false,
}
layout.annotations.push(result)
}
const [plotData1] = useState({ data, layout, style })
return (
<Plot {...plotData1} />
)
}
I imported react-multi-carousel module and set the simple carousel.
But it's not working. There are no <li> tags.
Rendered HTML is below.
<ul class="react-multi-carousel-track " style="transition:none;overflow:unset;transform:translate3d(0px,0,0)"></ul>
<div className={classes.profileCarousel}>
<Carousel
additionalTransfrom={0}
arrows={false}
autoPlaySpeed={0}
centerMode={false}
className={classes.carousel}
customButtonGroup={<ButtonGroup/>}
customDot = {<CustomDots/>}
dotListClass={classes.customDotList}
draggable
focusOnSelect={false}
infinite={true}
keyBoardControl
minimumTouchDrag={80}
partialVisible
renderButtonGroupOutside
renderDotsOutside
responsive={responsive}
showDots
sliderClass=""
slidesToSlide={1}
swipeable
>
{profiles.map(element => {
return <ProfileCard props={element} key="as"/>
})}
</Carousel>
</div>
stack: next.js typescript material-ui
What's wrong with it?
I already had this problem with this lib.
Basically it is because the brake points have not been set correctly.
This is a standard object I created, try using it and see if it works for you.
export const carouselBreakPoints = {
desktopWide: {
breakpoint: {
max: 2560,
min: 1421,
},
items: 6,
partialVisibilityGutter: 40,
},
laptopM: {
breakpoint: {
max: 1420,
min: 0,
},
items: 5,
partialVisibilityGutter: 40,
},
laptopL: {
breakpoint: {
max: 1150,
min: 0,
},
items: 4,
partialVisibilityGutter: 30,
},
laptop: {
breakpoint: {
max: 940,
min: 0,
},
items: 3,
partialVisibilityGutter: 30,
},
tabletL: {
breakpoint: {
max: 750,
min: 0,
},
items: 1.7,
partialVisibilityGutter: 30,
},
mobile: {
breakpoint: {
max: 400,
min: 0,
},
items: 1,
partialVisibilityGutter: 30,
},
};
(Solved): Solution was compatibility issues between chart.js and the react version. The recent update to the react version solved it.
I was having trouble finding the solution to this, maybe someone will know why this error is happening:
I am passing an object array via props, but I'm getting an error when it comes to using the prop:
Object array sample (The data for score is fed in through an API and is working):
var teamData = [
{
teamName: "Cowboys",
score: 0,
color: "rgba(0, 34, 68, 0.9)",
},
{
teamName: "Cardinals",
score: 0,
color: "rgba(135, 0, 39, 0.9)",
},
{
teamName: "Patriots",
score: 0,
color: "rgba(0, 21, 50, 0.9)",
},
{
teamName: "49ers",
score: 0,
color: "rgba(170, 0, 0.9)",
},
App.js example
const App = () => {
getData();
return (
<div>
<Header />
<BarChart teamData={teamData} />
<Calculator teamData={teamData} />
<Footer />
</div>
);
};
export default App;
And here is where I pass the props to insert one by one into a chart.js component({props[0].score} is where the error is, it says: SyntaxError C:\Users\name\Documents\Websites\React Based\madden-ranker\src\components\BarChart.js: Unexpected token, expected "," (14:37):
import React from "react";
import { Bar, HorizontalBar } from "react-chartjs-2";
const BarChart = (props) => {
return (
<HorizontalBar
// prettier-ignore
data={{
labels: ['Team1' ],
datasets: [
{
label: 'Cumulative Score',
data: [{props[0].score}, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 10, 20],
backgroundColor: []
},
],
}}
height={500}
width={600}
options={{
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
},
},
}}
/>
);
};
export default BarChart;
Interestingly, I tried console logging it inside the teamData component and got this:
line5: console.log(props);
line6: console.log(props.teamData[0].score);
.
Just add a condition to check if props[0] exists before rendering, cause the first time the props is empty, and when you setstate after getting data from API the component rerenders. Hopefully this helps.
I think you had a few commas in places that they shouldn't be
Try this
const BarChart = (props) => {
return (
<HorizontalBar
// prettier-ignore
data={{
labels: ['Team1' ],
datasets: [
{
label: 'Cumulative Score',
data: [{props[0].score}, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 100, 19, 3, 5, 2, 3, 5, 2, 3, 10, 20],
backgroundColor: []
}
]
}}
height={500}
width={600}
options={{
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
}
}
}}
/>
);
};
I am trying to create a polar chart using the highcharts-react-official HighchartsReact wrapper. However, I get a regular line chart or errors. How do I use the highcharts/highcharts-more features with the HighchartsReact wrapper?
import React from 'react';
import {render} from 'react-dom';
import Highcharts from 'highcharts';
import HighchartsMore from 'highcharts/highcharts-more'
import HighchartsReact from './HighchartsReact';
HighchartsMore(Highcharts)
const options = {
chart: {
type: 'polar',
},
title: {
text: 'Kahn Chart'
},
pane: {
startAngle: 0,
endAngle: 360
},
xAxis: {
tickInterval: 90,
min: 0,
max: 360,
labels: {
format: '{value}°'
}
},
yAxis: {
min: 0,
max: 10,
},
plotOptions: {
series: {
pointStart: 0,
pointInterval: 90
},
column: {
pointPadding: 0,
groupPadding: 0
}
},
series: [
{
type: 'area',
name: 'c1',
data: [7.15, 10, 9.85, 8.81],
},
{
type: 'line',
name: 'c2',
data: [4.65, 3.16, 4.30, 4.48],
},
{
type: 'line',
name: 'Fair Value',
data: [4.77,6.67, 6.57, 5.87],
}
]
};
const App = () => (
<div>
<HighchartsReact highcharts={Highcharts} options={options} />
</div>
);
render(<App/>, document.getElementById('root'));
You need to change:
chart: {
type: 'polar',
},
To:
chart: {
polar: true
},
API Reference: https://api.highcharts.com/highcharts/chart.polar