I made a servlet that creates a map object:
Map<String, Integer> data = new LinkedHashMap<String, Integer>();
fills in the data and returns a response in JSON format using google JSON:
String json = new Gson().toJson(data);
All this works great when retrieving data and repeating it into a table. But I need it in a special format for the Highcharts plugin:
series: [{ name: 'Monday', data: [10] }, { name: 'Tuesday', data: [20] }, { name: 'Wednesday', data: [30] }, { name: 'Thursday', data: [40] }, { name: 'Friday', data: [50] }, { name: 'Saturday', data: [60] }, { name: 'Sunday', data: [70] }]
To do this, you need to create a script as shown below:
$(document).ready(function() { var options = { chart: { renderTo: 'container', defaultSeriesType: 'column', rightMargin: 80 }, title: { text: 'Weekdays' }, subtitle: { text: 'Source: somewhere in a calendar' }, xAxis: { labels: { enabled: false } }, yAxis: [ { min: 0, title: { text: 'Amount' } }, { linkedTo: 0, opposite: true } ], series: [] }; $.getJSON('WeekdayServlet', function(data) { var series = []; $.each(data, function(key, value) { series.name = key; series.data = value; options.series.push(data); });
Anyway, I'm doing something wrong. Either in an iteration, or how to βinitializeβ a series .
Here are my sources for a better understanding: