nicolaskruchten / pivottable

Open-source Javascript Pivot Table (aka Pivot Grid, Pivot Chart, Cross-Tab) implementation with drag'n'drop.
https://pivottable.js.org/
MIT License
4.33k stars 1.07k forks source link

Timeseries support + cumulated data over time for c3 line+area #655

Open pankewen opened 7 years ago

pankewen commented 7 years ago

Dear Nicolas, your work here is just impressive. Thanks a lot, I really like it! :-)

I am using your code but missed a feature: sometimes I have data which containes dates. These could be used in line and area diagrams using timeseries axis. Currently your code is fixed to "category". The disadvantage is that all x ticks are equally distributed.

I extended your c3_renderer.js and want to share it. Perhaps you intend to take it over:

          if (chartOpts.type === "scatter") {
               [...] 
            } else {
               // new cord: for line and area charts: check, if x axis can be shown as timeseries:
               var xAxisType = "default";
               if ((chartOpts.type === "line") || (chartOpts.type === "area")) {
                  if (isNaN(Date.parse(headers[0]))) {
                     xAxisType = "default";
                  } else {
                     xAxisType = "timeseries";
                  }
               }
               // new code for timeseries support:
               if (xAxisType === "timeseries") {
                  // timeseries
                  params.axis.x.type = 'timeseries';//'category';
                  params.axis.x.tick = {
                     format: '%Y-%m-%d',
                     rotate: 90,
                     fit: false
                  };
                  params.data.x = 'x';
                  headers.unshift('x');
                  columns.unshift(headers);
                  params.data.columns = columns;
               } else {
               // original code:
                  params.axis.x.type = 'category';
[...]

And then, using this a next point come up :-) For timeseries it would be great to have the possibility to show cumulated data. This means the data should sum up from left to right (from oldest date to newest one). I will investigate this a bit. In case I extend it, I will let you know also.

So my question: are planning to proved timeseries support in some future?

Kind regards and have a nice weekend! Chris

pankewen commented 7 years ago

Sorry for the format of the code snippet. I uploaded a .txt which is more readable. samplecode.txt

nicolaskruchten commented 7 years ago

Thanks for this!

I have considered this approach in the past and have always found it a bit too brittle... Relying on whether or not Date.parse returns NaN for the first value of a series seems dangerous to me, especially since any number will parse as a date. In addition, Date.parse has odd behaviour for various commonly-used formats: for example new Date(Date.parse("10-10-19")) returns "Thu Oct 10 2019 00:00:00 GMT-0400 (EDT)" whereas new Date(Date.parse("2010-10-19")) returns "Mon Oct 18 2010 20:00:00 GMT-0400 (EDT)".

I wonder if a more flexible approach wouldn't be to accept a function to do this detection, so that developers could "choose their poison", so to speak, if they know they are passing in dates in MM/DD/YY or YYYY-MM-DD format etc.

nicolaskruchten commented 7 years ago

Regarding the idea of cumulative sums, you may want to check out issue #140 :)

nicolaskruchten commented 7 years ago

(oh, and as a PS: pasting multi-line blocks of code in Github is usually done surrounded by triple-backticks, to preserve formatting. You can optionally add a language as I did above :)