I am trying to create a Highcharts Bar Chart visualization in JS Fiddle. It will visualize "Annual Guest Value" and "Days Between 1st and 2nd Visits" for each category of visits by for customer (1 visit, 2 visits, .... 29+ visits).
Right now I have coded the visualization JS Fiddle but you must manually enter in the data. I would like to make a more automated process. The data that I will have is in JSON form and I was wondering how I would be able to create some function that would import the JSON form of the data to visualize it the same way as a bar chart I currently have right now.
How would I do this?
The JSON data I have now is:
[
{
"Category": 1,
"Q1 / 18 (TTM) Annual Guest Value": 0,
"Days Between 1st and 2nd Visits": 23
},
{
"Category": 2,
"Q1 / 18 (TTM) Annual Guest Value": 96.6,
"Days Between 1st and 2nd Visits": 45
},
{
"Category": 3,
"Q1 / 18 (TTM) Annual Guest Value": 73.2,
"Days Between 1st and 2nd Visits": 65
},
{
"Category": 4,
"Q1 / 18 (TTM) Annual Guest Value": 60.3,
"Days Between 1st and 2nd Visits": 85
},
{
"Category": 5,
"Q1 / 18 (TTM) Annual Guest Value": 52.5,
"Days Between 1st and 2nd Visits": 105
},
{
"Category": 6,
"Q1 / 18 (TTM) Annual Guest Value": 46.6,
"Days Between 1st and 2nd Visits": 125
},
{
"Category": 7,
"Q1 / 18 (TTM) Annual Guest Value": 41.4,
"Days Between 1st and 2nd Visits": 144
},
{
"Category": 8,
"Q1 / 18 (TTM) Annual Guest Value": 37.5,
"Days Between 1st and 2nd Visits": 163
},
{
"Category": 9,
"Q1 / 18 (TTM) Annual Guest Value": 34.4,
"Days Between 1st and 2nd Visits": 179
},
{
"Category": 10,
"Q1 / 18 (TTM) Annual Guest Value": 31.9,
"Days Between 1st and 2nd Visits": 199
},
{
"Category": "11-17",
"Q1 / 18 (TTM) Annual Guest Value": 25.4,
"Days Between 1st and 2nd Visits": 258
},
{
"Category": "18-28",
"Q1 / 18 (TTM) Annual Guest Value": 17,
"Days Between 1st and 2nd Visits": 394
},
{
"Category": "29+",
"Q1 / 18 (TTM) Annual Guest Value": 9.7,
"Days Between 1st and 2nd Visits": 847
}
]
My current code for the bar chart is below. I want the visualization to look the same, but just that the data comes from JSON file.
Copying and pasting the JSON data into the code in order to make the visualization is fine with me but I just want the visualization I have now to be the same.
I would really appreciate the help. Thanks!
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: null,
align: 'center',
verticalAlign: 'bottom',
},
xAxis: {
categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11-17', '18-28', '29+'],
title: {
text: 'Visits Per Customer (TTM)'
},
},
yAxis: {
min: 0,
gridLineWidth: 0,
minorGridLineWidth: 0,
title: {
text: 'Average Return Rate Overall: 64 Days',
y: 10
},
labels: {
overflow: 'justify'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y:.0f} </b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
bar: {
dataLabels: {
enabled: true,
}
}
},
legend: {
layout: 'horizontal',
align: 'right',
verticalAlign: 'top',
x: -25,
y: 5,
floating: true,
borderWidth: 1,
backgroundColor: ((Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'),
shadow: true
},
credits: {
enabled: false
},
series: [{
name: 'Q1 / 18 (TTM) Annual Guest Value',
data: [0, 96.6, 73.2, 60.3, 52.5, 46.6, 41.4, 37.5, 34.4, 31.9, 25.4, 17.0, 9.7],
color: 'grey',
// label color
dataLabels: {
style: {
color: 'grey'
}
}
}, {
name: 'Days Between 1st and 2nd Visits',
data: [23, 45, 65, 85, 105, 125, 144, 163, 179, 199, 258, 394, 847],
color: 'green',
// label color
dataLabels: {
style: {
color: 'green'
}
}
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto"></div>