dimple - Ploting annual series as stacked lines - javascript

I am having a hard time attempting to stack yearly time series with dimple.js.
For each year, one line should be printed, representing the cumulative amounts from January 1st to December 31.
[{"year":"2018","month":"1","day":"15","date":"2018-01-15","amount":"60.00","cumulative_amount":"60.00"},
{"year":"2018","month":"1","day":"29","date":"2018-01-29","amount":"20.00","cumulative_amount":"80.00"},
{"year":"2018","month":"2","day":"18","date":"2018-02-18","amount":"150.00","cumulative_amount":"230.00"},
{"year":"2018","month":"3","day":"10","date":"2018-03-10","amount":"10.00","cumulative_amount":"240.00"},
{"year":"2018","month":"4","day":"13","date":"2018-04-13","amount":"30.00","cumulative_amount":"270.00"},
(...)
{"year":"2019","month":"1","day":"4","date":"2019-01-04","amount":"40.00","cumulative_amount":"40.00"},
{"year":"2019","month":"1","day":"25","date":"2019-01-25","amount":"10.00","cumulative_amount":"50.00"},
{"year":"2019","month":"3","day":"12","date":"2019-03-12","amount":"225.00","cumulative_amount":"275.00"},
(...)
]
Based on the records above, the desired plot should look like:
But the current plot looks like:
var svg_cumulated_amounts= dimple.newSvg("body", 1400, 400);
var amounts= [{ The json data comes here. }];
chart_cumulated_amounts = new dimple.chart(svg_cumulated_amounts, amounts);
chart_cumulated_amounts.setBounds(65, 30, 1330, 305);
// var x = chart_cumulated_amounts.addTimeAxis("x", "date", "%Y-%m-%d", "%m");
var x = chart_cumulated_amounts.addCategoryAxis("x", "month");
x.addOrderRule("month");
// x.addOrderRule(["1","2","3","4","5","6","7","8","9","10","11","12"]);
// x.addGroupOrderRule("day");
// x.addGroupOrderRule("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
y = chart_cumulated_amounts.addMeasureAxis("y", "cumulated_amount");
chart_cumulated_amounts.addSeries("year",dimple.plot.line);
chart_cumulated_amounts.addLegend(500,10, 510, 20);
chart_cumulated_amounts.draw();
The GroupOrderRule, which would be useful in the case of a bar plot does not lead to the desired result in this situation.
I believe one cause of the problem is that the x-axis is set for categories and consequently are the records grouped by month.
I want the records to remain ungrouped, and the lines to follow each data point.
Basically, months do not matter. They are only to display the x-axis as desired.
Setting the x axis as a time axis, does not seem to me a priori as a solution: then, how would I stack the lines?
I wonder if I should maybe structure the data output differently, or stack several graphs in the same SVG plot, or any other solution.
I could find several quite similar cases, but none of the suggestions found here and there was truly applicable.
Thank you for your help.

Related

Remove weekends from highcharts

Is it possible to remove certain days in highcharts? I have a chart that only get data intervals from Monday to Friday. The problem Is that Saturday and Sunday is auto added to the graph even when there is no data for these days. I cant find anything that helps on api.highcharts.com they usually have solutions to all graph related problems but I cant seem to find anything about my problem. It's probably some easy option in the chart but I cant find anything that works.
You have two options, use highcharts and breaks.
An array defining breaks in the axis, the sections defined will be left out and all the points shifted closer to each other.
Requires that the broken-axis.js module is loaded.
It would look something like this, in a datetime axis:
xAxis: {
tickInterval: 1,
breaks: [{
from: 1537567200000,
to: 1537740000000,
breakSize: 1
}]
}
Working example: http://jsfiddle.net/ewolden/L3ykegzq/
Or switch to highstock where you can have an ordinal axis (ordinal is used by default):
In an ordinal axis, the points are equally spaced in the chart regardless of the actual time or x distance between them. This means that missing data periods (e.g. nights or weekends for a stock chart) will not take up space in the chart. Having ordinal: false will show any gaps created by the gapSize setting proportionate to their duration.
Working example: http://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/stock/xaxis/ordinal-true/

Set y-axis maximum label to one tick increment higher in NVD3

I am using NVD3 (a wrapper of d3) to draw a line graph. I want the data in the graph to be within the range of the axis. However, it looks inconsistent with the other labels, as the chart displays the max value of my data set on its own. See screenshot:
In this exanple, 18,554.41 is my highest data point. What I would like to see is the ticks/axis-lables to be in the same order of rounding throughout, with no overflow. i.e. 20,000,18000,16000 etc.
The caveat is that my dataset can vary quite differently - so I can't just set a max. Is there a way of just increasing the tick count by one or something?
Current relevant code:
var chart = nv.models.lineChart()
.showYAxis(true)
.forceY([0]);
chart.yAxis
.axisLabel('£')
.tickFormat(d3.format(','))
.ticks(8);
EDIT: added https://jsfiddle.net/60equ79h/2/
on the fiddle, I would like the first data set's topmost label to be 10,000. the second would be 80. i.e I would like to the y-axis to be increased by one tick
If I understand your question correctly, you can force the yAxis to have the values you define. You can hard code the values or write something clever to identify the min & max for your yAxis.
Update your chart to have the following:
chart.forceY([0, 20000]); // [min, max]
Hope it helps

How can I add a trace in Plotly, and set a custom order for each data point along the x axis?

I'm trying to create a line graph that has (x, y) coordinates where the x axis corresponds to a date and the y axis corresponds to a value. The date is formatted DD-MM-YYYY, i.e. 15-04-2015.
When I add my first trace with something like:
var trace = {
x: ["9-10-2016", "6-12-2016", "8-12-2016"],
y: [30, 11, 38],
name: "trace 1",
mode: 'lines
, I simply sort my array for the x axis and y axis by date using a custom sort method for a javascript array.
But when I add a new trace to the graph, there doesn't seem to be an obvious way to have the points ordered by date relative to the other traces on the graph. They seem to always be to the right of everything on the first trace, even if some of the points in the second trace have dates that are earlier than the dates in the first trace.
How are you implementing your custom sort method for the x-axis? Is this something that Plotly supports? I didn't see any documentation for this.
According to this page, Plotly’s date format is yyyy-mm-dd HH:MM:SS.ssssss.
I would try to have the data for the x-axis match this format and see if it resolves your problem. If it doesn't resolve your problem, it would be helpful to have a JSFiddle that demonstrates the problem.

NVD3 Line chart tics wrongly aligned to grid when using datetype on xAxis

I am currently working with NVD3 using Angular Directive (angular-nvd3). I have a very simple line chart with very simple data.
The problem I have encountered now is that my data is wrongly aligned with the Axis. Example plunker available here: http://plnkr.co/edit/jWEYt6?p=preview ,
I am using dates on my xAxis, which are parsed using d3 library:
tickFormat: function(d) {return d3.time.format('%d/%m')(new Date(d))}
Description:
I would expect the xAxis labels to be correspondent to the grid.
In the example you can clearly notice that the xAxis is not evenly devided (values: 06/11, 08/11, 11/11, 13/11). So usually 2 days and sometimes 3 days :)
What is worse - the peaks are not matching the grid. Example: 06/11 tick is really not even close to the grid's line where I guess it is supposed to be.
I have also tried this on master's code from repo and it happens there too. There is a link in the HTML head section.
Is there a problem with my data, proper date formatting or something else? Thanks!
This bugged me for a while and I could not find an answer here. I even have opened a bug on GitHub: https://github.com/novus/nvd3/issues/1382#issuecomment-160694559 and I was clued in on the answer.
The problem:
The actual issue is hidden because of d3.time.format('%d/%m'). My example data is given in one tick per day manner, and the format was set accordingly. But d3 does not understand that. When drawing the grid it divides the max-min/someValue and the grid ticks does not have to occur on full day (midnight), but on any hour. And because of the formatting I could not see that.
The version showing this misconception is here: http://plnkr.co/edit/2iMHOp?p=preview
Solution:
So now, when I know what I could do, I managed to substitute the ticks by using tickValues parameter in nvd3 / angular wrapper.
The version with the solution is here:
http://plnkr.co/edit/23n3ll?p=preview
Yet another bug :)
Funny thing is that since the labels are too long to be displayed, I had to rotate them so they could fit. Another bug occurs here (I think). As you can see 2nd and last but one tick label is missing. First I tried using the solution mentioned here: NVD3 Line Chart X Axis Ticks Are Missing using the showMaxMin parameter but it does not work correctly. But if you rotate the labels to ~ -70 degrees the labels are displayed OK.
I guess this is not the end with my NVD3 journey ;)
Since the problem is, according to Atais:
The actual issue is hidden because of d3.time.format('%d/%m'). My example data is given in one tick per day manner, and the format was set accordingly. But d3 does not understand that. When drawing the grid it divides the max-min/someValue and the grid ticks does not have to occur on full day (midnight), but on any hour. And because of the formatting I could not see that.
I managed to pass the x's values as integer values (ex: 20160211) instead of formatted dates (ex: 2016-02-11 or similars) to nvd3, and then on tickFormatformat them to display properly.
I wrote another plunker with the problem and the commented solution (used momentjs):
Plunker with the simulated error: http://plnkr.co/edit/fXDQ0f?p=preview
Data is provided in format x: milliseconds, y: int, like {x: 1446418800000, y: 20}, and it is being formated with tickFormat:
xAxis: {
tickFormat: function(d) {
return moment(d).format('YYYY-MM-DD');
}
}
Plunker with the solution: http://plnkr.co/edit/KpALzo?p=preview
Data is provided in format x: int, y: int, like {x: 20160211, y: 20}, and it is being formated with tickFormat:
xAxis: {
tickFormat: function(d) {
moment(d, 'YYYYMMDD').format('YYYY-MM-DD');
}
}
Note that you can do it with time too, just by appending to the 'numeric date'.
As stated from #ajaybc, will not work well with dates from different months, since d3 will interpolate X axis with invalid filling dates (days 32, 33, 34 and so on)

Strange tooltip behavior with long series in highcharts

I'm using Highcharts for a project in which I have to display two series with about a thousand points each. The x-axis represents a date, and the y-axis a quantity. In addition, each point has an associated list of namesMy data is day-by-day without gaps, with a structure such as
var mydata = [ ...
{x: theDate, y: theValue, names: theNames},
... ]
where theNames is an array of strings. I can access these in the tooltip formatter through this.points.point.names, given that the range displayed on the chart is small enough. If I change the x-axes so that the start date and end date are more than roughly a year apart, then the tooltip is not rendered at all.
One of the possible avenues that I have tried but failed with so far is setting the turboThreshold limit to the length of the longest series plus 1. Setting this lets me at least display a graph when mydata.length > 1000 (the default value). However, this only displays the tooltip if the x-axis range is less than 261. Otherwise, the tooltip disappears entirely, as does the point.data object where I'm getting the name from.
I'm also not great at JavaScript, but I was wondering if there were a way to separate the names of the points from the array containing them (in my examples, myData1 and myData2) and somehow access those names from the tooltip function without going through the current point.
Here is the link to the jsFiddle demonstrating this issue.
All help is appreciated!
The problem is in dataGrouping, when disabled works fine: http://jsfiddle.net/34tfg/1/
DataGrouping is method in Highcharts to approximate points and display them when width of the chart is not enough, e.g. how to display 10 000points in a chart of width 1 000px -> 10 points in a one pixel..? And when dataGrouping is used, new points are created, so all your custom options like 'names' etc. are lost (at least not accessible).
Code:
plotOptions: {
line: {
dataGrouping: {
enabled: false
},
turboThreshold: 10000
}
},

Categories