Going off of what I found here, I am able to quite easily produce a horizontal timeline chart. What I'd like to do now is add an additional line graph (or multiple add'l graphs) to overlay that timeline. The timeline is going to show promotions (ads) running over a date range and I need to display a line graph of SALES, for example, over the entire date range.
If I do this, I get the promos plotted out on a timeline successfully:
var d1 = [[(new Date("2016-12-01")).getTime(), 1], [(new Date("2016-12-10")).getTime(), 1]];
var d2 = [[(new Date("2016-12-12")).getTime(), 2], [(new Date("2016-12-20")).getTime(), 2]];
var d3 = [[(new Date("2016-12-21")).getTime(), 3], [(new Date("2016-12-31")).getTime(), 3]];
var plot = $.plot($chartDiv, [d1,d2,d3], {
xaxis:{mode:"time", timeformat:"%Y-%m-%d"},
yaxis:{ticks:[[1, "Promo 1"],[2, "Promo 2"],[3, "Promo 3"]]}
});
That produces something like this: https://i.sstatic.net/nPGtF.png
I've tried adding the sales data and configuring the options in numerous ways to no avail. Here is the closest that I've come:
var d1 = [[(new Date("2016-12-01")).getTime(), 1], [(new Date("2016-12-10")).getTime(), 1]];
var d2 = [[(new Date("2016-12-12")).getTime(), 2], [(new Date("2016-12-18")).getTime(), 2]];
var d3 = [[(new Date("2016-12-16")).getTime(), 3], [(new Date("2016-12-24")).getTime(), 3]];
var d4 = [d1,d2,d3];
var d5 = [
[(new Date("2016-12-01")).getTime(),201000],
[(new Date("2016-12-02")).getTime(),201000],
[(new Date("2016-12-04")).getTime(),356897],
[(new Date("2016-12-05")).getTime(),201000],
[(new Date("2016-12-06")).getTime(),545122],
[(new Date("2016-12-07")).getTime(),100324],
[(new Date("2016-12-08")).getTime(),348645],
[(new Date("2016-12-09")).getTime(),201000],
[(new Date("2016-12-10")).getTime(),729646],
[(new Date("2016-12-11")).getTime(),206456],
[(new Date("2016-12-12")).getTime(),357713],
[(new Date("2016-12-15")).getTime(),280647],
[(new Date("2016-12-17")).getTime(),398654],
[(new Date("2016-12-18")).getTime(),412054],
[(new Date("2016-12-19")).getTime(),167545],
[(new Date("2016-12-20")).getTime(),512345],
[(new Date("2016-12-21")).getTime(),612454],
[(new Date("2016-12-22")).getTime(),760214],
[(new Date("2016-12-23")).getTime(),715546],
[(new Date("2016-12-24")).getTime(),650654],
[(new Date("2016-12-25")).getTime(),712547],
[(new Date("2016-12-26")).getTime(),780242],
[(new Date("2016-12-27")).getTime(),725454],
[(new Date("2016-12-28")).getTime(),799142]
]
var chartData = [
{
data:d4,
yaxis:1
},
{
data:d5,
yaxis:2
}
];
// plot it
var plot = $.plot($chartDiv, chartData, {
xaxis:{mode:"time", timeformat:"%Y-%m-%d"},
yaxes: [
{
position:"left",
ticks:[[1,"Promo 1"],[2,"Promo 2"],[3,"Promo 3"]],
max: 4
},
{
position: "right",
max: 800000,
}],
grid: {
mouseActiveRadius: 50,
hoverable: true,
clickable: true
}
});
The result is this: https://i.sstatic.net/7Q2T5.png
I just can't seem to get the promos to show up. Is it because the two data sets are configured differently? If so, how do I do that properly? Do I need to "fill in" the dates for the promos so it looks like this:
var d1 = [
[(new Date("2016-12-01")).getTime(), 1],
[(new Date("2016-12-02")).getTime(), 1],
[(new Date("2016-12-03")).getTime(), 1],
[(new Date("2016-12-04")).getTime(), 1],
[(new Date("2016-12-05")).getTime(), 1],
[(new Date("2016-12-06")).getTime(), 1],
[(new Date("2016-12-07")).getTime(), 1],
[(new Date("2016-12-08")).getTime(), 1],
[(new Date("2016-12-09")).getTime(), 1],
[(new Date("2016-12-10")).getTime(), 1]
];
(I'm pretty sure I've tried that as well.)
You need to modify the format of your chartData
array to fall in line with what flot expects as the data format. Instead of defining multiple data sets as one series (d1
, d2
, & d3
as d4
), each data set is it's own series:
var chartData = [
{
data:d1,
yaxis:1
},
{
data:d2,
yaxis:1
},
{
data:d3,
yaxis:1
},
{
data:d5,
yaxis:2
}
];
This JSFiddle has a working example of your chart. If you want the the line colors of d1
, d2
, and d3
to match, you can specify that in your new chartData
object.