React Highcharts piechart show tooltip on click outside of highcharts container - reactjs

I am having a "Donutchart" using highcharts and a selfmade legend for that chart. When i click on an element of the legend, i want to trigger the "mouseOver" or "click" event of that container, so that the chart shows the respective area, belonging to the element of the legend. Basically i want the tooltip to show up.
The Piechart Code:
let chartOptions = {
chart: {
plotBackgroundColor: null,
plotBorderWidth: 0,
plotShadow: false,
},
title: {
text: `Title`,
},
plotOptions: {
pie: {
dataLabels: {
enabled: false,
distance: -125,
y: -5,
format: "{y}%",
style: {
fontWeight: "bold",
color: "black",
fontSize: "12px",
},
},
borderWidth: 3,
},
series: {
animation: false,
},
},
tooltip: {
shared: true,
},
series: [
{
type: "pie",
name: "",
tooltip: {
valueDecimals: 2,
valueSuffix: " USD",
},
innerSize: "60%",
data: data_array,
},
],
};
Example button:
<div className='mb_3px mt_3px cursor_pointer color_blue'
onClick={() => {
handleSimulateMouseOver(
item.index
);
}}
>
{truncate(
object[
item.index
].name,
39
)}
</div>
In older posts i found a function called setState('hover'), which i can't bring to work in react, that looks like the following:
let handleSimulateMouseOver= (index) => {
chartOptions.series[0].data[index].setState('hover');
chartOptions.tooltip.refresh(chartOptions.series[0].data[index]);
};

There is an event to trigger callback after legend item click - API: https://api.highcharts.com/highcharts/series.line.events.legendItemClick
Have you tried to use it?

Related

How to move outside pie chart labels on angular apexcharts.js

I'm trying to move the labels out of the pie chart, but I can't move them in any way, is there any way i angular to move them out of the pie chart? (Angular v14)
I don't quite understand the documentation so it would be great if someone could help me understand how to do this.
Typescript code:
#ViewChild('chart') chart!: ChartComponent;
public chartOptions: Partial<ChartOptions>;
constructor() {
this.chartOptions = {
series: [45, 50, 22],
chart: {
type: 'pie',
width: '270px',
},
labels: ['Team A', 'Team B', 'Team C'],
responsive: [
{
breakpoint: 480,
options: {
chart: {
width: 100,
},
},
},
],
legend: {
show: true,
position: 'bottom',
horizontalAlign: 'center',
},
dataLabels: {
offSetY: 50,
// add this part to remove %
enabled: true,
},
};
}
HTML Code:
<div class="chartContainer" style="width: 30%;">
<apx-chart
[series]="chartOptions.series"
[chart]="chartOptions.chart"
[labels]="chartOptions.labels"
[responsive]="chartOptions.responsive"
[legend]="chartOptions.legend"
[dataLabels]="chartOptions.dataLabels">
</apx-chart>
</div>
I tried to put "offset", "offsetY", etc, inside the datalabel, but they didn't move an inch
You can tweak dataLabels and plotOptions.pie.dataLabels in your chartOptions. I give you a basic example (without Angular for practical reasons...):
let options = {
series: [45, 50, 22],
chart: {
type: 'pie',
height: 350
},
labels: ['Team A', 'Team B', 'Team C'],
dataLabels: {
style: {
colors: ['black']
}
},
plotOptions: {
pie: {
dataLabels: {
offset: 60
}
}
}
};
let chart = new ApexCharts(document.querySelector('#chart'), options);
chart.render();
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
<div id="chart"></div>

chartjs-plugin-zoom: get currently visible values when the page first loads

I have a chart component called genStarts whose view depends on the visible data of the following chart (singleChart). I'm using the onZoomComplete and onPanComplete options to insert my getVisibleValues function so that genStarts can be updated on zooms and pans. However, I have no way to get the currently visible values when the page first starts up. My getVisibleValues function does not work outside of the chartStyle object, and I haven't found a chart option that executes on startup.
In other words, my genStarts graph is only populated when SingleChart is zoomed or panned but I want it to be populated immediately.
const SingleChart = React.memo(({ setVisible }) => {
function getVisibleValues({ chart }) {
setVisible(chart.scales.x.ticks);
}
const chartStyle = {
options: {
animation: false,
maintainAspectRatio: false,
responsive: true,
plugins: {
zoom: {
zoom: {
wheel: {
enabled: true,
modifierKey: "shift",
},
pinch: {
enabled: true,
},
enabled: true,
drag: true,
mode: "x",
onZoomComplete: getVisibleValues,
},
pan: {
enabled: true,
mode: "x",
speed: 2,
onPanComplete: getVisibleValues,
},
mode: "xy",
},
legend: {
display: false,
},
},
scales: {
y: {
type: "linear",
display: "true",
position: "left",
grid: {
drawBorder: true,
color: "#000000",
},
ticks: {
beginAtZero: true,
color: "#000000",
},
title: {
display: yAxisLabel != "",
text: yAxisLabel,
},
},
x: {
max: 9,
grid: {
drawBorder: true,
color: "#00000",
},
ticks: {
beginAtZero: false,
color: "#000000",
},
},
},
},
};
// ... some code to get chartData
return (
<div>
<Line
data={chartData}
options={chartStyle.options}
width={20}
height={195}
/>
</div>
);
});
export default SingleChart;
You can make use of the animations, they have a property to check if its the first time they have fired, although since you dont want animations you will need to set the main one to a verry low number and at least the tooltip to false and the transition duration of active elements to 0.
const getVisibleValues = ({
chart
}) => {
console.log(chart.scales.x.ticks.map(el => ({
value: el.value,
label: el.label
}))) // Map because stack console prints whole circular context which takes long time
}
const options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderColor: 'pink'
}]
},
options: {
animation: {
onComplete: (ani) => {
if (ani.initial) {
getVisibleValues(ani)
}
},
duration: 0.0001
},
transitions: {
active: {
animation: {
duration: 0
}
}
},
plugins: {
tooltip: {
animation: false
}
}
}
}
const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>

How to dynamically emphasis a specific category on a bar chart with a background on echarts-for-react

I'm using echarts and i'm trying to emphasis the "current picked bar" by adding background color.
I want to be able to click on one of the bars, and by doing so the month will change and the background color will be applied on the back of that month.
Makes sense?
Here's a codesandbox that emphasis my idea:
https://codesandbox.io/s/flamboyant-cloud-zy44j?file=/src/App.js
But going over and documentation it seems as though there is no option to add backgroundColor to just one category / bar. I tried using another series but that did not work.
I'm also attaching pictures to explain what should be.
And also attaching the code.
import React, { useState } from "react";
import "./styles.css";
import ReactEcharts from "echarts-for-react";
import moment from "moment";
export default function App() {
const [month, setMonth] = useState(0);
const onChartClick = (params) => {
const monthClicked = moment().month(params.name).month();
setMonth(monthClicked);
};
console.log(month);
const renderChart = () => {
const _onEvents = {
click: onChartClick
};
const option = {
maintainAspectRatio: false,
tooltip: {
trigger: "item"
},
grid: {
left: "0px",
right: "0px",
bottom: "0px",
top: "0px",
containLabel: false,
show: false
},
xAxis: {
position: "top",
axisLine: {
show: false
},
axisTick: {
show: false
},
axisLabel: {
inside: true,
color: "#74818f",
fontFamily: "SegoePro-Regular",
fontSize: 12
},
splitNumber: 1,
splitLine: {
show: true,
lineStyle: {
color: [
"#ffffff",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#eaeaea",
"#ffffff",
"#ffffff",
"#ffffff"
]
}
},
type: "category",
data: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
]
},
yAxis: {
scale: true,
type: "value",
axisLabel: {
show: false
},
axisTick: {
show: false
},
axisLine: {
show: false,
onZero: false
},
splitLine: {
show: false
}
},
series: [
{
name: "Monthly Income",
type: "bar",
barWidth: "30%",
barGap: "-20%",
label: {
show: true,
position: "top",
fontSize: 12,
fontFamily: "SegoePro-Bold",
color: "#3d70ff"
},
itemStyle: {
opacity: 0.7,
color: "#3d70ff"
},
emphasis: {
itemStyle: {
opacity: 1
}
},
tooltip: {},
data: [
8700,
8700,
10400,
8699,
8699,
8699,
8699,
8699,
11643.46,
0,
0,
0
],
markArea: {
silent: true,
data: [
[
{
coord: [0, 0]
},
{
coord: [100, 100]
}
]
],
itemStyle: {
color: "#f5f7fa"
},
label: {
show: false
}
}
}
]
};
const chartStyle = 280;
return (
<div id="chart_div">
<div className="chart-wrapper">
<ReactEcharts
onEvents={_onEvents}
option={option}
style={{ height: chartStyle }}
/>
</div>
</div>
);
};
return renderChart();
}
the requirement is unachievable with this library. you can make x-axis clickable by triggerEvent and do something with it; but adding a background or any other style on a "clicked" bar ( or axis ) needs a dedicated state; clicked state; which this library doesn't have; it listens to click events, but doesn't keep it on individual elements; so you can't style it based on a state that doesn't exist;
I was thinking about a workaround to make it happen by some applying css styles on clicked/hovered bars to fake the effect somehow but all the content are rendered inside a canvas, so no option remains; either use another library or write a chart drawer component yourself or forget this specific styling based on click;

Highchart: Break y-axis doest work in react app but is working in js fiddle

using same options in js-fiddle and react app y-axis break is not working in react app but is working fine in js-fiddle.
highchart version i am using is 7.2.1
[enter link description here][1] jsfiddle where break is working
Highcharts.chart('container', {
chart: {
height: 250,
animation: false,
borderColor: '#EFF3F5',
plotBorderColor: '#EFF3F5',
plotBorderWidth: 1,
style: {
},
marginLeft: undefined,
},
legend: {
enabled: false,
},
credits: {
enabled: false,
},
title: {
text: '',
},
subtitle: {
text: '',
},
xAxis: [{
categories: [],
type: 'datetime',
crosshair: {
color: '#96abb6',
width: 1,
snap: false,
},
labels: {
style: {
fontSize: '10px',
color: '#334252',
fontFamily: 'DecimaMono',
textTransform: 'uppercase',
lineHeight: '12px',
whiteSpace: 'nowrap',
},
formatter: function() {
return this.value;
},
},
alternateGridColor: '#F7F9FA',
}, ],
yAxis: [{
tickPositioner: function() {
const positions = [];
let tick = Math.floor(this.dataMin);
const max = Math.min(200, this.dataMax);
const increment = Math.ceil((max - this.dataMin) / 6);
if (this.dataMax !== null && this.dataMin !== null) {
for (tick; tick - increment <= max; tick += increment) {
positions.push(tick);
}
}
if (this.dataMax > 200) {
positions.push(this.dataMax);
}
return positions;
},
title: {
text: null,
},
labels: {
style: {
fontSize: '10px',
textAlign: 'right',
},
},
breaks: [{
from: 200,
to: 1700,
}],
}, ],
series: [{
name: 'Attraction 1',
data: [-0.3543, 5.4184, -31.3792, 95.2435, 135.5852, 104.7914, 84.5844, 8.5129, -38.4724, -54.1816, -13.1134, 677.986, 1763, 1420.0503, 760.9013, 100.8341, 10.4576, 89.8975, 97.4758, 55.4993, 51.4611, 24.1278, 9.9771, 26.9394, 22.042, 32.9894, 145.3526, 88.1315, 135.0617, 119.6472, 29.8568, 43.94, 26.4247, 43.4719, 128.6346, 119.7356, 33.2159, 58.6534, -7.6348, 2.1865, 31.7992],
color: '#e63c45',
lineWidth: 3,
}, ]
});
[enter image description here][2]
[2]: https://i.stack.imgur.com/TIKkO.png screenshot from react app
I reproduce your code in the React environment and everything looks fine, same as in the jsFiddle.
Demo: https://codesandbox.io/s/highcharts-react-demo-83jnr?file=/demo.jsx

React Highstock Won't Work But Highchart works

I would like to switch from React Highcharts to React Highstock because of the compare option I would like to use in Highstock. Here is my config:
var config = {
credits: {
enabled: false
},
title: {
text: '' // No Title
},
chart: {
height: 200,
type: 'line',
backgroundColor: 'rgba(155, 255, 255, 0.0)' // Always transparent Background
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2,
split: true
},
yAxis: {
labels: {
format: '$ {value}' // Always Money
},
title: {
text: '' // No Label
}
},
xAxis: {
type: 'datetime',
labels: {
format: '{value:%Y\'%b}'
},
tickInterval: getTickInterval(this.props.interval),
},
plotOptions: {
series: {
color: transformColor(this.props.color),
lineWidth: 1,
compare: 'percent',
showInNavigator: true
},
line: {
marker: {
enabled: false
}
}
},
legend: {
layout:'vertical',
align: getLegendPosition(this.props.legend).align,
verticalAlign: getLegendPosition(this.props.legend).verticalAlign,
x: getLegendPosition(this.props.legend).x,
y: getLegendPosition(this.props.legend).y,
floating: true,
itemStyle: {
font: 'sans-serif',
fontWeight: 'bold',
fontSize: '9px'
},
symbolHeight: 10,
symbolWidth: 10
},
series: this.props.series
};
return (
<div>
<ReactHighcharts config={config}/>
</div>
)
}
But when I switch to Highstock:
return (
<div>
<ReactHighstock config={config}/>
</div>
)
It gives me an error:
InvalidCharacterError: Failed to execute 'createElement' on 'Document': The tag name provided ('/static/media/Highstock.a88db6d5.jsx') is not a valid name.
Any ideas?
Nevermind, don't actually use the React-Highstock npm package. Just do this:
const ReactHighstock = require('react-highcharts/ReactHighstock');

Resources