Kendoui bar chart set series on click of a button - javascript
I am working with KendoUI bar chart. Please check this fiddle. In this fiddle I am setting series config at the design time. I want to add series to the chart at run time on click of a button. How can I add series to kendo chart on click of a button.
Currently series is assigned at the design time, i want to do the same at the run time on click of a button.
Code:
<!DOCTYPE html>
<html>
<head>
<base href="http://demos.telerik.com/kendo-ui/bar-charts/grouped-stacked-bar">
<style>html { font-size: 14px; font-family: Arial, Helvetica, sans-serif; }</style>
<title></title>
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.common-material.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.1.112/styles/kendo.material.min.css" />
<script src="//kendo.cdn.telerik.com/2016.1.112/js/jquery.min.js"></script>
<script src="//kendo.cdn.telerik.com/2016.1.112/js/kendo.all.min.js"></script>
</head>
<body>
<div id="example">
<div class="demo-section k-content wide">
<div id="chart"></div>
</div>
<script>
function createChart() {
$("#chart").kendoChart({
title: {
text: "World population by age group and sex"
},
legend: {
visible: false
},
seriesDefaults: {
type: "column"
},
series: [{
name: "0-19",
stack: "Female",
data: [854622, 925844, 984930, 1044982, 1100941, 1139797, 1172929, 1184435, 1184654]
}, {
name: "20-39",
stack: "Female",
data: [490550, 555695, 627763, 718568, 810169, 883051, 942151, 1001395, 1058439]
}, {
name: "40-64",
stack: "Female",
data: [379788, 411217, 447201, 484739, 395533, 435485, 499861, 569114, 655066]
}, {
name: "65-79",
stack: "Female",
data: [97894, 113287, 128808, 137459, 152171, 170262, 191015, 210767, 226956]
}, {
name: "80+",
stack: "Female",
data: [16358, 18576, 24586, 30352, 36724, 42939, 46413, 54984, 66029]
}, {
name: "0-19",
stack: "Male",
data: [900268, 972205, 1031421, 1094547, 1155600, 1202766, 1244870, 1263637, 1268165]
}, {
name: "20-39",
stack: "Male",
data: [509133, 579487, 655494, 749511, 844496, 916479, 973694, 1036548, 1099507]
}, {
name: "40-64",
stack: "Male",
data: [364179, 401396, 440844, 479798, 390590, 430666, 495030, 564169, 646563]
}, {
name: "65-79",
stack: "Male",
data: [74208, 86516, 98956, 107352, 120614, 138868, 158387, 177078, 192156]
}, {
name: "80+",
stack: "Male",
data: [9187, 10752, 13007, 15983, 19442, 23020, 25868, 31462, 39223]
}],
seriesColors: ["#cd1533", "#d43851", "#dc5c71", "#e47f8f", "#eba1ad",
"#009bd7", "#26aadd", "#4db9e3", "#73c8e9", "#99d7ef"],
valueAxis: {
labels: {
template: "#= kendo.format('{0:N0}', value / 1000) # M"
},
line: {
visible: false
}
},
categoryAxis: {
categories: [1970, 1975, 1980, 1985, 1990, 1995, 2000, 2005, 2010],
majorGridLines: {
visible: false
}
},
tooltip: {
visible: true,
template: "#= series.stack #s, age #= series.name #"
}
});
}
$(document).ready(createChart);
$(document).bind("kendo:skinChange", createChart);
</script>
</div>
</body>
</html>
Ageonix gave a good answer! If you do not want to use a global variable for the chart options, you can get the chart instance from the DOM data-attribute, add the series to the chart instance and then tell the chart to redraw:
var newseries = {
name: "100+",
stack: "Female",
data: [654622, 625844, 784930, 844982, 900941, 39797, 72929, 118435, 118454]
};
//Get the chart
var chart = $("#chart").data("kendoChart");
//add the series
chart.options.series.push(newseries);
//refresh the chart
chart.redraw();
Updated DOJO
If you set the chart options as a global variable, you can then modify options.series and set it to your updated series data source in the button click event and call createChart() again, which will update (recreate) the chart with the updated series.
Create a global chartOptions variable:
var chartOptions =
{
title: {
text: "World population by age group and sex"
},
legend: {
etc. etc.
Then create the chart with the options stored in the variable:
$("#chart").kendoChart(chartOptions);
Finally, update the options.series with the updated data and recreate the chart:
click: function()
{
chartOptions.series = updatedSeries;
$("#chart").kendoChart(chartOptions);
}
I updated your Dojo and added a button that populates a new series on click.
http://dojo.telerik.com/oTeTi/3
Related
Highcharts radio button toggle between charts
I'm trying to use radio buttons to toggle the display between two Highchart charts (different data series) using divs. I essentially need the accepted solution here: Mootools Highcharts radio button toggle between charts, working jQuery version ...but I can't work out how to specify the variables for each chart from my existing code. The above solution uses this format: var chart1 = new Highcharts.Chart({ chart: { renderTo: 'divID-1', height: 400, ... var chart2 = new Highcharts.Chart({ chart: { renderTo: 'divID-2', height: 400, ... whereas each of my charts are specified in the following format, e.g. Chart 1: Highcharts.setOptions({ lang: { decimalPoint: '.', thousandsSep: ',' } }); $.get('data_stackedarea_value.csv', function(csv) { $('#divID-1').highcharts({ chart: {type: 'area'}, data: {csv: csv}, ... How can I translate these into variables that can be called by the initial toggle function? window.addEvent('domready', function() { document.getElements("[name=toggler]").addEvent('click', function(){ $$('.toHide').setStyle('top', '-9999em'); $$('.toHide').setStyle('opacity', '0'); $("divID-"+this.get('value')).setStyle('top', '0').fade(0,1); }); Many thanks
Here's an example where I adopted the solution from 23817700 to use the way you are passing variables to the charts. I couldn't resist also adapting the toggle code to use jQuery, since it was required already. You can try this is action in this fiddle provided by #RachelGallen: https://jsfiddle.net/ezc7oghm/1/ <!doctype html> <html> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <body> <div id="graphwrap" style="height: 410px;"> <div id="divID-1" class="toHide" style="position:relative;margin-bottom:-400px;"></div> <div id="divID-2" class="toHide" style="position:relative;top:-9999em;opacity:0;"></div> </div> <div id="viewSelectWrap"> <h4>View Select</h4> <label><input id="rdb1" type="radio" name="toggler" value="divID-1" style="cursor:pointer;" checked/>1</label> <label><input id="rdb2" type="radio" name="toggler" value="divID-2" style="cursor:pointer;" />2</label> </div> <script> $(function() { $('[name=toggler]').click(function () { $('.toHide').css({ top: '-9999em', opacity: 0 }); var chartToShow = $(this).val(); $('#' + chartToShow).css({ top: 0, opacity: 1 }); }); $('#divID-1').highcharts({ chart: { type: 'bar' }, title: { text: 'Fruit Consumption' }, xAxis: { categories: ['Apples', 'Bananas', 'Oranges'] }, yAxis: { title: { text: 'Fruit eaten' } }, series: [{ name: 'Jane', data: [1, 0, 4] }, { name: 'John', data: [5, 7, 3] }] }); $('#divID-2').highcharts({ chart: { type: 'bar' }, title: { text: 'Time spent on hobbies' }, xAxis: { categories: ['Skiing', 'Bicycling', 'Swimming'] }, yAxis: { title: { text: 'Time spent on hobbies' } }, series: [{ name: 'Jane', data: [1, 0, 4] }, { name: 'John', data: [5, 7, 3] }] }); }); </script> </body> </html>
Highchart Series Data Format for Google Spreadsheet
I'm attempting to use Highcharts Series function to create a combination bar/line chart, but I'm having trouble just getting the series to display. There's nothing in the API about how to format data from Google Spreadsheets when using series so I took a stab at it and the chart does not display: $(function() { Highcharts.setOptions({ chart: { backgroundColor: '#fff', shadow: false, width: null, height: null } }); $('#ms-96-enrollment').highcharts({ series: [{ type: 'bar', data: [{ googleSpreadsheetKey: '1Nx8zcIi0ULxytLmra0A9N11-llzJCDVH2-7SbK_k5-U', startColumn: 0, endColumn: 1, startRow: 0, googleSpreadsheetWorksheet: 7 }], }] }); }); <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/data.js"></script> </head> <div> <div id="ms-96-enrollment"></div>
Highcharts is a little bit misleading having two "data"s - in series and on top level. The first is used for javascript point arrays while the later is for facilitating adding existing datasets (http://api.highcharts.com/highcharts#data) Thus for Google Spreadsheets $('#ms-96-enrollment').highcharts({ chart: { type: 'bar' }, data: { googleSpreadsheetKey: '1Nx8zcIi0ULxytLmra0A9N11-llzJCDVH2-7SbK_k5-U', startColumn: 0, endColumn: 1, startRow: 0, googleSpreadsheetWorksheet: 7 }, title: { text: 'My google data' }, yAxis: {}, xAxis: { labels: { enabled: true, } } }); Here's the sample based on your data http://plnkr.co/edit/aIqMVcaeyYEbHcdxoMaT UPDATE If you need to show multiple series add extra columns and specify series: data: { googleSpreadsheetKey: '1Nx8zcIi0ULxytLmra0A9N11-llzJCDVH2-7SbK_k5-U', startColumn: 0, endColumn: 2, startRow: 0, googleSpreadsheetWorksheet: 7 } ... series: [{ type: 'bar' }, { type: 'line' }] Here's the updated sample http://plnkr.co/edit/UQprlugBtUXQX9OffUm4?p=preview
Highcharts not displaying data but drawing chart when reading from CSV file
I have spent many hours learning JavaScript and HighCharts today. I am trying to get this chart to display. I managed to get the JSON to work but I would prefer to get the CSV to work as all of my files are in CSV. I have gone through the JavaScript debugger a lot and see that my data is saving correctly but yet the data does not draw. The axis and title do display. Attached is my code. Thanks in advance for help. For whatever reason, the JSFiddle doesn't work so feel free to look at what I mean at: www.teratuple.com/data/dataVolume.html <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> </head> <body> <div id = "container" style = "width:100%; height:400px;"></div> <script> var seriesData = []; var chart; $(document).ready(function () { var options = { chart: { renderTo: 'container' }, rangeSelector: { buttons: [{ type: 'hour', count: 1, text: '1h' }, { type: 'day', count: 1, text: '1d' }, { type: 'month', count: 1, text: '1m' }, { type: 'year', count: 1, text: '1y' }, { type: 'all', text: 'All' }], selected: 1 // all }, xAxis: { title: { text: 'Date' } }, yAxis: { title: { text: 'Volume (mm3)' } }, title: { text: 'Volume Differential' }, series: [{ data: [], tooltip: { pointFormat: '{series.name}: <b>{point.y}</b><br/>', valueDecimals: 2 } }] }; $.get('www.teratuple.com/data/volumeData.csv', function (data) { var lines = data.split('\n'); $.each(lines, function (lineNo, line) { var items = line.split(','); if (lineNo > 0) { seriesData.push([parseInt(items[0]), parseFloat(items[1])]); } }); options.series.data = seriesData; chart = new Highcharts.StockChart(options); }); }); </script> </body> </html>
The series property is meant to be an array so set your series data via options.series[0].data = seriesData;
how to enable drawing multiple lines on highstock basic-line graph?
I want to draw multiple line for this type of graph of your library : http://www.highcharts.com/stock/demo/basic-line I found this sample on internet: http://jsfiddle.net/yildirim_timur/Hb3Q7/ Below you can see my html file. I tried to do couple of things but couldn't make it. How can i make my chart to be able to draw multiple lines as well? (it is for an ipad app project) <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Senior Project Timur Aykut YILDIRIM - IH Technology</title> <script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script> <link rel="stylesheet" type="text/css" href="/Users/ihtechnology/Desktop/chart_deneme/css/normalize.css"> <link rel="stylesheet" type="text/css" href="/Users/ihtechnology/Desktop/chart_deneme/css/result-light.css"> <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet"/> <script type='text/javascript'> var serviceDataURL = "http://xx.xx.xxx.xxx:83/get_item_data_ios?generic="; function setDictionary(x){ return x; } // no need for this method var dict = "web service query string will be here"; $(function() { $.getJSON(serviceDataURL.concat(dict), function(data) { // Create the chart window.chart = new Highcharts.StockChart({ chart : { renderTo : 'container' }, navigation: { buttonOptions: { enabled: false, width: 60 } }, rangeSelector : { buttonSpacing: 20, buttonTheme: { // styles for Q,Y,YTD,ALL buttons fill: 'none', stroke: 'none', 'stroke-width': 15, style: { color: '#039', fontWeight: 'bold' }, states: { hover: {}, select: { fill: '#039', style: { color: 'white' } } } }, selected : 3, // 3=ALL buton at first inputDateFormat: '%Y-%m-%d', inputEditDateFormat: '%Y-%m-%d', buttons:[ { type: 'month', count: 3, text: 'QQ' }, { type: 'year', count: 1, text: 'YY' }, { type: 'ytd', text: 'YTD' }, { type: 'all', text: 'ALL' }, ] }, title : { text : 'My Total Market' }, credits: { text: " ", href: " ", }, series : [{ name : 'Total Market', data : arr, tooltip: { valueDecimals: 2 } }], exporting: { enabled: false } }, function(chart){ // apply the date pickers setTimeout(function(){ $('input.highcharts-range-selector').attr('readonly',1); // burda webviewı engelledik $('input.highcharts-range-selector', $('#'+chart.options.chart.renderTo)) },0) }); }); }); //]]> </script> </head> <body> <div id="container" style="height: 500px; min-width: 500px;"></div> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> <script src="http://code.highcharts.com/stock/highstock.js"></script> <script src="http://code.highcharts.com/stock/modules/exporting.js"></script> </body> </html>
Right now you have: series : [{ name : 'Total Market', data : arr, tooltip: { valueDecimals: 2 } }] So you have just one object inside series. If you want multiple series then should be something like that: series : [{ name : 'Total Market I', data : arr_1, tooltip: { valueDecimals: 2 } },{ name : 'Total Market II', data : arr_2, tooltip: { valueDecimals: 2 } }] Edit: To add multiple series, push them to array: var mySeries = []; mySeries.push({ name : 'Total Market I', data : arr_1 }); mySeries.push({ name : 'Total Market II', data : arr_2 }); mySeries.push({ name : 'Total Market III', data : arr_3 }); Then create chart: series: mySeries
Adjusting the appearance of a javascript- Shield UI Chart
I have a problem with Shield UI Chart. I need to hide all text on my X axis, that is dateTime. But i can't visualize the chart when I add the code that hides the axis. Below is the code that I am using. Will appreciate any assistance fixing it. <script type="text/javascript"> $(function () { $("#chart").shieldChart({ axisX: { axisTickText: { enabled: false } axisType: 'datetime' }, axisY: { title: { text: "Total Sales statistics" } }, primaryHeader: { text: "Aaron's cars sales" }, dataSeries: [{ seriesType: 'bar', collectionAlias: 'New Cars Sales Volumes', data: [325000, 425000, 316000, 378000, 390000] }, { seriesType: 'bar', collectionAlias: 'Used Cars Sales Volumes', data: [125000, 175000, 158000, 130000,101000,98000] }] }); }); </script>
You have forgotton to put a comma between the different properties. You have axisTickText:{} and there must be a comma, because there is one more property following- axisType. axisX: { axisTickText: { enabled: false }, axisType: 'datetime' },