Getting highcharts to re-animate a line graph - javascript

I would like to create a highCharts page with a button to "re-animate" the chart.
When I initially create the series, I set the 'redraw' to FALSE:
$("contain").highcharts().addSeries({
name: "filter",
data: result
}, false, {duration: 300000, easing: 'linear'});
When I then manually call redraw() on the entire chart:
$("contain").highcharts().redraw()
...the chart animates from the start - BUT ONLY ONCE. After the charts are animated 'on-screen', I've found no way to have them re-animate from the start EXCEPT FOR REMOVING AND THEN RE-ADDING, not an ideal solution for my case as there will be many series (lines) & ALOT of data such that I was hoping there was a simpler solution (I see somebody answered while I editted the question)...
Is there any way to do this or will have to remove and then re-add the series each time I want to re-animate?
Here is the fiddle I promised earlier. It's using the remove/re-add concept which I do not love : http://jsfiddle.net/bhilleli/854jbbhg/

If you want to reanimate the same series, you will want to remove your current one and redraw it as a new series.
$('#button').click(function() {
$("contain").highcharts().series[0].remove();
$("contain").highcharts().addSeries({
name:"filter",
data: result} );
});

Related

Multiple Highcharts JavaScript Update very slow. Any options to increase speed?

I have multiple Highcharts on the same page, and I update them all at the same time using a combobox which selects the individual to be shown at the charts.
All the Highcharts are quite simple, with 3-4 line or column series each, and a limited number of observations per chart (around 10-12).
For updating the charts I use a JS function, and I've tried two options:
First option:
chart1.update({
series: [{data: mybankjson["data"]["spr_t_tva"]},
{data: mybankjson["data"]["enf_t_tva"]},
{data: mybankjson["data"]["hogares_t_tva"]}],
title: {text: 'Tasas de variación interanual del crédito. SPR<br>('+ecode+'-'+anombre+')'}
});
Second option:
chart1.series[0].setData(mybankjson["data"]["spr_t_tva"])
chart1.series[1].setData(mybankjson["data"]["enf_t_tva"])
chart1.series[2].setData(mybankjson["data"]["hogares_t_tva"])
chart1.setTitle({text:'Tasas de variación interanual del crédito. SPR<br>('+ecode+'-'+anombre+')'})
There are around 40 charts to be updated using first or second option. Both are really slow (around .5 seconds EACH chart), so using the combobox to quickly view charts for different individuals is mainly impossible.
Any ideas? The size of the .json file (mybankjson) might be an issue?

How I can manipulate with legend items on the chart from AgChartsReact?

I use a standalone chart from the Ag Grid library and want to manipulate manually which legend items should be shown/selected by default. I didn't find any specific API/property which can cover this use-case.
E.g., I want to see selected only the diesel legend item (and petrol should be shown unselected),
AgChartsReact always shows all legend items.
What did I investigate and try?
The chart is a canvas element, so I thought that it is possible to manipulate legend items from the canvas context, which I can get with useRef hook. But I cannot find how exactly it can be done and is it possible? Here is a ref to Plunker
UPD:
if someone is looking for how to store the data which legends is active or not, here is a function to get this (the same function you will find in Plunker):
const getLegendStatuses = () => {
const [changeEvent] = chartRef.current.chart.legend.allEventListeners.get("change");
const [, data] = changeEvent;
const [event] = data;
console.log(
event.data.map(({ enabled, label }) => ({ label: label.text, enabled }))
);
}
I think you want to use visible: false on the Petrol series.
This will still show up as greyed out in the legend, but you can click it to open it again.
You can also add showInLegend: false if you want to remove it from the legend.
If you want it greyed out in the legend but NOT clickable on the legend to make it visible, you might be able to make a custom listener for nodeClick, but I haven't tried that.

ChartJS showing wrong dataset following visibility property toggle

In my project I have I have ten graphs in a table with each graph showing two datasets.
I want to toggle the visibility of each dataset by clicking it which is default behaviour in ChartJS.
All the charts are held in an array and following the built in toggle method of ChartJS, every chart is updated to the dataset of the first chart. So to be clear, when the page loads, each chart is shown correctly, but if I made the dataset invisible and visible again, it becomes visible but with the wrong data.
I tried writing my own update function to toggle the visibility.
var option = {
legend: {
onClick: ToggleDatasetVisibility,
}}
and then
function ToggleDatasetVisibility() {
this.chart.getDatasetMeta(1).hidden = !this.chart.getDatasetMeta(1).hidden;
// this.chart.update();
}
However exactly the same thing happens. After the update,
*Note, I'm only toggling the second dataset of each DatasetMeta so that's why the 1 is hardcoded in this example. That's not the chart in question.
In case it's useful, we is the code when the charts are primed with data when the page loads and here the update shows the correct data.
for (var i = 0; i < 10; i++) {
var MyArray= await fetch("http://localhost:1234/Demo/FetchResults?model_id=" + i);
//Add the recorded tunnel data to dataset 0
LineChartArray[i].data.datasets[0].data = ResultsArray;
LineChartArray[i].data.datasets[0].label = '';
LineChartArray[i].data.datasets[1].data = MyArray;
LineChartArray[i].data.datasets[1].label = 'LabelXYZ';
LineChartArray[i].update(); //This update updates the graph with the correct data.
}
}
So, I don't know if there's a bug with chart JS or a correct determine which chart to update but I discovered that the render method works. This updates the chart following the visibility toggle loads the correct data back in when made visible. I thought it might be my use of the 'this' command but it works fine with the render method.
I just updated the toggle function like so,
function ToggleDatasetVisibility() {
this.chart.getDatasetMeta(1).hidden = !this.chart.getDatasetMeta(1).hidden;
this.chart.render();
}

c3 chart.load with multiple charts

I am using c3.js and c3.css to make several graphs on one page. I have multiple charts and want to update the data every 60 seconds so I use set interval, get the updated data, and then load it to a chart, however, it puts all of the data in the most recently made chart. I attempted to add a bindto to the data so it goes to the correct graph, but it still loads onto the bottom graph. how can I fix this?
...
<div id='chart1'></div>
<div id='chart2'></div>
...
function update()//this function collects the new data in arrays cols and xrows
...
setInterval(function(){
update();
chart.unload();
chart.load({bindto:'#chart1',columns:cols, xs:xrows});
},3000);
For showing update data atfirst
hide previous data's DIV
Use CLEAR to for cleaning Data
Again Load the DIV
If anyone's still looking to do this I've found a solution.
When you generate each chart, the declaration ends with:
}); }, chart = generate();
If you rename chart like so:
}); }, first_chart = generate();
Then you can manipulate that chart like so:
first_chart.load ({ <some_stuff> })
Give each chart variable a distinct name and you can edit them distinctly.

amCharts pie chart how to customize the balloon text

I am using amCharts pie chart (v3) and I want to customize the pop up balloon that comes on mouseover on a slice: in addition to routine text like [[value]] [[percent]],
(a) a text that depends on the data item, and
(b) provide different click options to the user depending on data item. Every data row might not have these possibilities in which case the options should not appear.
For example, the pie chart might show wine consumption by country. On hover, I want to provide the user two different click options - one to see consumption by season if available, plus another to see consumption by age group if available.
Is it possible to do so in amCharts? I saw an exit for event clickSlice() here http://docs.amcharts.com/3/javascriptcharts/AmPieChart but nothing on how to modify the balloon text on the fly and add behavior to the balloon that comes on hover.
Will greatly appreciate your help.
For writing dynamic text in the balloon you can also do the folowing:
"graphs": [{
"balloonFunction": function(graphDataItem, graph) {
var value = graphDataItem.values.value;
if (value < 500) {
return value + "<br>(Little)";
} else {
return value + "<br>(A Lot)";
}
}
}],
You can use the following tags: [[title]], [[description]], [[value]] and [[percent]]. says the documentation.
I have used description for a custom field, it works fine.
You can add any custom field to your data provider and then display it in your balloon in the same way as value:
{value:15, title:"slice title", someCustomField:"some custom data"}
and then:
chart.balloonText = "[[someCustomField]]";

Categories