I've used setting label on start and its working, here is the working code:
var chartDonut1 = c3.generate({
data: {
columns: [
['data1', 30],
['data2', 120],
],
type : 'donut'
},
donut: {
title: "title"
}
});
But, i want to change the label on the donut based on search, and for that purpose i need to use load function.
EDIT:
Here is example of donut chart and with title: LINK
How can label be change/set on load?
I've tried setting like code bellow, but i can't make it work:
chartDonut1.load({
json: dataJsonGraph,
donut: {
title: 'title1',
},
bindto: '#chartDonut1'
});
Any suggestion will be great!
I think the bindto: '#chartDonut1' should be declared inside the generate function.
It seems you cannot change dynamically graph's title inside load function (In c3js documentation, no reference to it).
You will need to select the chart using d3 instance, then change the title. Example:
var label = d3.select('#chart2 text.c3-chart-arcs-title');
label.html(''); // remove existant text
label.insert('tspan').text('30').attr('dy', 0).attr('x', 0).attr('class','big-font');
label.insert('tspan').text('Test Data').attr('dy', 20).attr('x', 0);
A way to change it is to edit html's code : http://plnkr.co/edit/ew5dDA1R3biXnMp80LDd?p=preview
Related
basically, I have made a Graph that looks like this Image.
I want to make the Graph update whenever I choose an option ( a location in this example). Any idea how could I go on about this?
It sounds like you need to update the chart data whenever the user selects a value from the dropdown. So, add an event listener to the location selector dropdown element, then replace the chart data with the data for the selected location, then call chart update. That part is very simple. It's explained in the chart.js docs under Updating Charts. https://www.chartjs.org/docs/latest/developers/updates.html
For example, if you had location data in an object and a transformData function to convert it to x/y coordinates, you might do something like this for a line chart:
const defaultLocation = 'munchen';
const chart = new Chart(chartCanvas,
{
type: 'line',
data: transformData(locationData[defaultLocation]),
options: {
animation: false,
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true,
},
x: {
type: 'timeseries',
},
},
},
});
const locationSelector = document.getElementById("location-selector");
locationSelector.addEventListener('change', ({ target }) => {
chart.data = transformData(locationData[target.value]);
chart.update();
});
I'm trying to dynamically add annotations to my high charts in React. I'm using the addAnnotation function to add a new annotation whenever a hover event is triggered on my app, but the annotation does not render. I dropped a debugger into my code, and when I call chart.annotations I can see there is currently an array of annotations, but they are not rendering. I even have make a call to the addPlotLine in this function and the plotline is rendered on the chart. My config file looks like this
chart: {
annotations: [{
labelOptions: {
backgroundColor: 'rgba(0, 0, 0, 0.5)',
verticalAlign: 'top',
align: 'right',
}
}],
annotationsOptions: {
},
.... some lots more config options
}
and my on hover function to add the annotation is as follows
if( isNumber(value) )
//this renders a plotline
chart.xAxis[0].addPlotLine(baseChartHandle.generateInteractivePlotLine(value));
// this doesn't render my annotation however
chart.addAnnotation({
linkedTo: value,
title: {
text: "It works!"
}
});
}
I found that when I added annotations using Highcharts that the "linkedTo" function never worked despite my trials.
Despite adding a guid to my point, it never was able to apply it. I would suggest in your case adding it by x and y value, then finding the point that way instead. Here is how I have added annotations in the past successfully:
series.data.forEach(function (point) {
var annotationObject =
{
id: point.id,
labelOptions: {
y: 15,
verticalAlign: 'bottom',
distance: 25
},
labels: []
};
}
var text = <get text you want to push>;
annotationObject.labels.push(
{
point: {
xAxis: 0,
yAxis: 0,
x: point.x,
y: point.y
},
text: text
}
);
_chart.addAnnotation(annotationObject);
});
I also found a bug in HighCharts when using remove annotations, as a heads up. Each point will need an id like above, but it should be called by this line:
_chart.removeAnnotation(id);
It will remove the annotation, however you will get lots of complaints from highcharts if you try to do anything after, and it will break. I found the answer here, in annotations.js :
The red box is code I added. If you do removeAnnotation(ann.id), and it rerenders, it will fail because the labels object is null. Adding this check lets you do that without the labelCollectors failing.
Happy charting.
I am building a plotly surface that follows their online example here. I haven't been able to find an example of a 3D surface with custom axis labels like they have here. I tried putting an xaxis object with a title on the layout object then passing it as the third parameter to the Plotly.newPlot() command, but nothing rendered.
Does anyone know an example of this working or know where I can dig into or if it's even possible?
var layout = {
title: 'Surface Plot',
xaxis: {
title: 'This is a test'
}
};
Plotly.newPlot('someDiv', data, layout);
as far as i can tell, d-roy told you the whole truth in his comment yesterday:
var layout = {
title: 'Mt Bruno Elevation',
scene: {
xaxis: {
title: 'thisIsATest',
titlefont: {
color: 'red',
family: 'Arial, Open Sans',
size: 12
}
},
...
Works for me. See:
https://jsfiddle.net/27cfgLet/3/
I need to change the styling (background & size) of a Kendo UI line chart markers dynamically using Angular.
I know this is possible with a function like this:
markers: {
background:function(e) {
return 'blue';
}
This works great when I use it on my controller, or anywhere else outside the html file after
$("#chart").kendoChart(...);
I am working with Angular, so the series options is inside my html tag with k-serias directive.
When I try to add a function there I get an error.
Error: [$parse:lexerr] Lexer Error: Unexpected next character at columns 237-237 [#] in expression [[
Here is the directive I put:
k-series="[{
type: 'line',
field: 'score',
markers: {
type: 'square',
background: function(e) { return #ffffff; }
}
}]"
All I want to do is to set a function for both background and size attributes inside the k-serias directive.
Thanks!
You can bind the directive k-series to your angular $scope.series.
In the HTML file:
k-series="series"
In JS file
$scope.series = [{
type: 'line',
field: 'score',
markers: {
type: 'square',
background: function(e) { return "#ffffff"; }
}
}];
I have been going in circles for a few hours with this and have exhausted all the similar stackoverflow threads and also highcharts docs, so hopefully someone can help.
I am trying to plot a pie chart with a gender split. I have worked with line charts before and so had my data in the format for x and y axis, like this:
[{"y":"Mr","x":"145"},{"y":"Miss","x":"43"},{"y":"Mrs","x":"18"},{"y":"Ms","x":"2"}]
This was getting me somewhere, i could tap into the json and pull out the ints but i couldnt for the life of me get the titles associated with the figures...
function genderData() {
$.getJSON('/statsboard/gender', function(data_g) {
$.each(data_g, function(key_g, val_g) {
obj_g = val_g;
genderChart.series[0].addPoint(parseInt(obj_g.x));
//genderChart.xAxis[0].categories.push(obj_g.y);
});
});
}
I then just called the function genderData as follows:
genderChart = new Highcharts.Chart({
chart: {
renderTo: 'gender',
events: {
load: genderData
}
}
title: {
text: 'Gender Split'
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: false
},
showInLegend: true
},
},
series: [{
type: 'pie',
name: 'Gender Split',
data: []
}]
});
So i ended up with an accurate chart but with out the labels, and they would just default to 'slice'...
So close but no cigar ;-)
Soooo i altered my serverside code to return the following format as per the docs :-), now returning the following:
[{"Mr":"145"},{"Miss":"43"},{"Mrs":"18"},{"Ms":"2"}]
This looks pretty much spot on to me, but alas, when i try to accomodate for this on the js code, everything falls apart.
I have been looking at this:
Write JSON parser to format data for pie chart (HighCharts)
but cant get the practice applied here to fit my circumstances.. Can anyone help?
The forms:
[{"name": "Mr", "y": 145}, ...]
or
[["Mr", 145], ...]
will work. Notice the second form is an array of arrays not an array of objects (you were close).
See basic-pie demo (click view options and scroll down to data), series.data docs, and series.addPoint docs for more info.
If you make the server side return the data in one of the two above forms you can just do:
function genderData() {
$.getJSON('/statsboard/gender', function(data_g) {
genderChart.series[0].setData(data_g, true);
});
}
You can set your points as follows:
genderChart.series[0].addPoint({
name: key_g,
y: val_g
});
By rearranging my Json to have "name" and "y" as the keys i was able to make progress i.e:
[{"name":"Mr","y":"145"},{"name":"Miss","y":"43"},{"name":"Mrs","y":"18"},{"name":"Ms","y":"2"}]
Then by looping through the json data and parsing the "y" value as a int with the function parseInt() in the JS i was able to get my desired result...
function genderData() {
$.getJSON('/statsboard/gender', function(data_g) {
$.each(data_g, function(key_g, val_g) {
obj_g = val_g;
genderChart.series[0].addPoint({
name: obj_g.name,
y: parseInt(obj_g.y)
});
console.log(obj_g.y)
});
});
}
I know this question has been solved. but the map function for array might be a good choice in these cases, I love map in functional language!
data = [{"y":"Mr","x":"145"},{"y":"Miss","x":"43"},
{"y":"Mrs","x":"18"},{"y":"Ms","x":"2"}];
var series = data.map(function(e){ return parseInt(e.x); });
.... hight chart ....