New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposing isDatasetVisible method on chart object #15
Comments
Ideally, I would be able to call |
@niekvanderkooy of course we can this method as well. The AbstractChart has got the method The method /**
* Looks for the dataset that matches the current index and returns that metadata.
*
* @param index dataset index
* @return dataset meta data item.
*/
public DatasetMetaItem getDatasetMeta(int index) {
// checks consistency of chart and datasets
if (chart != null && data.getDatasets() != null && !data.getDatasets().isEmpty() && index < data.getDatasets().size()) {
// gets meta data item
DatasetMetaItem array = new DatasetMetaItem(getChartDatasetMeta(index));
// returns the metadata
return array;
}
// returns null
return null;
} The |
This was my first thought too. However, the This field is only initialised once the visibility is toggled by the user. Before this time, the javascript value is Null, and This difference is what chartJS solves in the isDatasetVisible function, see https://github.com/chartjs/Chart.js/blob/da3aa68f38b9db25156c3b32c56098f9ca70f4d8/src/core/core.controller.js#L731. |
I've seen an issue in CharJS where they explained that // Define a plugin to provide data labels
Chart.plugins.register({
afterDatasetsDraw: function(chart) {
var ctx = chart.ctx;
chart.data.datasets.forEach(function(dataset, i) {
var meta = chart.getDatasetMeta(i);
if (!meta.hidden) {
meta.data.forEach(function(element, index) {
// Draw the text in black, with the specified font
ctx.fillStyle = 'rgb(0, 0, 0)';
var fontSize = 16;
var fontStyle = 'normal';
var fontFamily = 'Helvetica Neue';
ctx.font = Chart.helpers.fontString(fontSize, fontStyle, fontFamily);
// Just naively convert to string for now
var dataString = dataset.data[index].toString();
// Make sure alignment settings are correct
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var padding = 5;
var position = element.tooltipPosition();
ctx.fillText(dataString, position.x, position.y - (fontSize / 2) - padding);
});
}
});
}
}); Let me say, sample and documentation should be managed better but I know which effort is needed for those.... and I can understand!! |
@niekvanderkooy I'm gonna add the following methods, currently public in chart:
I'll publish it asap |
@niekvanderkooy We have added and tested 3 methods above mentioned. See here isDatasetVisible in the master. |
@niekvanderkooy waiting for new release, this is a workaround. import org.pepstock.charba.client.AbstractChart;
import org.pepstock.charba.client.items.ChartNode;
import org.pepstock.charba.client.items.UndefinedValues;
public final class DatasetHelper {
private static DatasetHelper INSTANCE = new DatasetHelper();
private DatasetHelper() {
}
public static DatasetHelper get() {
return INSTANCE;
}
/**
* Looks for the dataset if it's visible or not, selected by index.
*
* @param index dataset index
* @return <code>true</code> if dataset is visible otherwise <code>false</code>.
*/
public boolean isDatasetVisible(AbstractChart<?, ?> chart, int index) {
ChartNode node = chart.getChartNode();
// checks consistency of chart and datasets
if (node.getId() != UndefinedValues.INTEGER && chart.getData().getDatasets() != null && !chart.getData().getDatasets().isEmpty() && index < chart.getData().getDatasets().size()) {
// gets if dataset is visible or not
return isChartDatasetVisible(chart, index);
}
// returns false
return false;
}
/**
* Returns the amount of datasets which are visible
*
* @return the amount of datasets which are visible. If chart is not initialized, return {@link org.pepstock.charba.client.items.UndefinedValues.INTEGER}.
*/
public int getVisibleDatasetCount(AbstractChart<?, ?> chart) {
ChartNode node = chart.getChartNode();
// checks consistency of chart and datasets
if (node.getId() != UndefinedValues.INTEGER) {
// gets if dataset is visible or not
return getChartVisibleDatasetCount(chart);
}
// returns false
return UndefinedValues.INTEGER;
}
/**
* Gets if the dataset is visible or not, selected by index.
* @param datasetIndex dataset index
* @return <code>true</code> if dataset is visible otherwise <code>false</code>.
*/
private native boolean isChartDatasetVisible(AbstractChart<?, ?> chart, int datasetIndex)/*-{
return chart.@org.pepstock.charba.client.AbstractChart::chart.isDatasetVisible(datasetIndex);
}-*/;
/**
* Gets the amount of datasets which are visible
* @return the amount of datasets which are visible
*/
private native int getChartVisibleDatasetCount(AbstractChart<?, ?> chart)/*-{
return chart.@org.pepstock.charba.client.AbstractChart::chart.getVisibleDatasetCount();
}-*/;
} Hopefully it can help Nevertheless I'm gonna add a method to |
In the master, isInitialized method. |
Charba 1.7 released with required methods. |
ChartJS exposes a
chart.isDatasetVisible(index)
method, to determine whether the dataset at an index is currently visible. It would be nice to expose this function in Charba.I am currently using
(This is after checking if the AbstractChart.chart field is not null, since this is not always the case), and I would expect a similar method on maybe
org.pepstock.charba.client.items.ChartNode
would do the trick?The text was updated successfully, but these errors were encountered: