ChartJS distinguish clicks between chart area, dataset and legend - reactjs

I have a react app that uses ChartJS through 'npm react-chartjs-2'.
I am working with a Line chart and I a have the following requirements:
1- Detect clicking on chart area (white area; other than data set line, and legend), then do some logic
2- Detect clicking on legend rectangle at the top to hide/show the dataset (current default behaviour)
I have tried using options.onClick but it seems to override legend click.
Also tried getElementAtEvent it returns the dataset clicked on but when clicking on legend/chart area comes as empty. getDatasetAtEvent always return empty array. And if I use options.legend.onClick I loose default hide/show behaviour and need to do it programmatically.
I was wondering is there is a better way to achieve requirements above. Thanks

You can use the getElementsAtEvent property. This gives value only when the chart area is clicked, the element is null if it is clicked on the legend. If it is clicked on the chart area then it holds properties from which we can get the clicked xAxis and legend value.
document.getElementById("yourChartCanvasId").addEventListener("click", function (e) {
var element = instance.getElementsAtEvent(e)[0];
if (element) {
//Handle click on chart area
}
else {
//Handle click on legend area
}
});
Also if you don't want to do anything on legend click, keeping the default Chart.js behavior then don't include the else block.

Related

Carousel with thumbnail images at the bottom

In a Codenameone app, I'm trying to develop a carousel with a thumbnail list at the bottom. I've used Tabs control to display files (of diff types like images, video, text, button etc) in carousel style in the center of a form and another Tabs control to display thumbanail images (of the first carousel files) at the bottom of the form. When a user selects a thumbnail image in the bottom carousel, corresponding component should be displayed in the first carousel.
hi.add(BorderLayout.CENTER, mainCarousel);
hi.add(BorderLayout.SOUTH, bottom_tab);
bottom_tab.addSelectionListener((i1, i2) -> {
// bottom_tab.getTabComponentAt(i2).addPointerPressedListener((i) -> {
mainCarousel.getTabComponentAt(i2).setVisible(true);
mainCarousel.getTabComponentAt(i2).repaint();
// });
});
But the component not getting displayed in the central carousel.
Also, I tried to capture the event addPointerPressedListener, but it's not getting fired when I select a thumbnail image.
You can't set tab components to visible/invisible to show/hide them. That won't work. I'm guessing that what you want is a horizontal list for the bottom UI similar to the answer here.
I would suggest using pointer released always. Notice that this will only get delivered to focusable components or the form. To make sure you get the event you can register a pointer release listener on the form.

Dygraphs 2.x legend posittioned off the right of graph

I have been trying to update our code to dygraphs 2.x from 1.1.1, but I have encountered a issue with the legend.
I believe it is related to the way our page is structured. We are also using React so this may also have an impact.
The dygraph is on a tab which is initially hidden until the user clicks a button after selecting various options and data sources to generate a time series line chart.
The legend option set to 'always' seems to push the legend off the right of the graph which is not readable by users.Unless they full screen the browser.
After debugging the source I can see that offsetWidth is being used to position the legend and is returning 0. I can only surmise that because the div the chart is inside is only made visible probably after the chart is drawn is messing the position of the legend.
If I regen the chart while visible the legend appears over top the chart as desired. But if I then hide the chart (by clicking on the other tab) and then show the chart (clicking on it's tab) the legend if off to the right again.
I'm not sure how to workaround this.
Presently I reverted back to 1.1.1 which does not have this issue.
Hope someone can suggest something.

chartjs - show hide specific dataset with click on element outside graph

The chartjs feature to show/hide a dataset by clicking on the legend is really nice. Is it possible to achieve the same but clicking on for instance a checkbox outside the chart canvas?
This should do the trick
chart.data.datasets.forEach(function(e) {
e.hidden = true;
});

kendo.resize for dataviz object

Issue with using kendo.resize inside the window.resize function. i need to bootstrap 3 functionalists for kendo chart, when i change the browser size so i used kendo.resize function. chart re sizing work perfectly only chart in visible area. as example i have kendo tab strip with two tab with 2 chart (tab 1 -chart1, tab 2-chart 2) when i click on tab 1 and re size the browser chart will re size, then go to tab 2 chart 2 is still in previous size when i re size the browser its again re size.
I suggest to look at example on kendo website:
http://demos.telerik.com/kendo-ui/bootstrap/
There is exactly what you request:
here is a sample code:
function resizeTabStripContent() {
kendo.resize("#tabstrip");
}
$("#tabstrip").kendoTabStrip({
animation: {
open: { effects: "fadeIn" }
},
activate: resizeTabStripContent
});
// resize nested charts when window resizes
$(window).resize(resizeTabStripContent);
It resizes charts in tab strip on window resize event and when tab is activated.

Extjs Charts: remove as series using chart.series.getAt(0) how?

Hello how can I remove a series from a chart, I know that I can get the series object using getAt(0) or getAt(1) depending on how many serieses I got in my chart.
my question is how can I remove a series from the chart.
main reason is that I give the end user the ability to change the series type: bar, line... so I wan to display the new change at run time by removing the series and adding a new one.
I tried Chart.series.removeAt(0); It removes the legend but the bar or the line still shown !!
I use extjs 4.1.1
thank you,
I had to do the same thing and give end users the ability to change the chart type at run time to give different visualizations of data. I found that there is more involved in the chart type (bar, line, pie) then just the series. In other words to achieve this, I wasn't able to just substitute a different series type for the data and add it to the chart.
I ended up creating a different config for each different type of chart using the same datastore. The chart was contained by an Ext.panel.Panel which I added this function to:
// toggle format between pie chart and bar chart
this.toggleFormat = function() {
var chart = me.down('chart');
this.remove(chart);
if (chart.chartType == 'pie') {
this.add(Ext.create('Ext.chart.Chart', bar));
} else {
this.add(Ext.create('Ext.chart.Chart', pie));
}
};
The bar variable holds a bar chart config and pie holds a pie chart config but both had the same datastore attached to their store: configs.
When the user pressed the format button I called this function to remove the chart from the panel and create one with the different format. It achieved the desired effect.

Resources