stitchfix / pyxley

Python helpers for building dashboards using Flask and React
MIT License
2.27k stars 257 forks source link

date time format %Y-%m-%d %H:%M:%S to create line chart #17

Open arvind111 opened 8 years ago

arvind111 commented 8 years ago

Hi Pyxley Team ,

first of all congratulation for the Wonderfull framework . I have one query here if i would like to create a line chart given in example metricsgraphics with date - time(x- axis) so what should be format of Date Column in the csv file for date - time(x axis) . dateparse = lambda x: pd.datetime.strptime(x, '%Y-%m-%d %H:%M:%S') df = pd.read_csv("fitbit_data.csv", parse_dates=['Date'], date_parser=dateparse) sf = df.set_index("Date").stack().reset_index() i have already tried but line charts failed with Date as %Y-% m-%d %H:%M:%S. line charts only works well with date format as %Y-% m-%d .

nperony commented 8 years ago

Hi, another Pyxley user here. At Tamedia Digital (Zurich), the data science team is planning to use Pyxley for quick dashboard implementation. We'd love to see datetimes supported for line charts. Thanks!

sanjeevbhalla commented 8 years ago

Another user who is using Pyxley and ran into this same issue. One should be allowed to specify the date format of the data.

nmkridler commented 8 years ago

I think the problem is that I'm casting a string to a Date. You should be able to format however you like, it just needs to be a string when it's transmitted to the javascript. I can probably force that in the default request function.

sanjeevbhalla commented 8 years ago

No. I tried string and it did not work. If I recall right the problem is that the MG date formatting function that is called from d3 expects a format and without a format passed it assumes a default of Y M and D. That is my recollection when I tried to understand whats going on.

Thanks

On Thu, Jan 7, 2016 at 8:23 AM, Nick Kridler notifications@github.com wrote:

I think the problem is that I'm casting a string to a Date. You should be able to format however you like, it just needs to be a string when it's transmitted to the javascript. I can probably force that in the default request function.

— Reply to this email directly or view it on GitHub https://github.com/stitchfix/pyxley/issues/17#issuecomment-169714488.

nmkridler commented 8 years ago

Here's the javascript code that does the conversion.

    _update: function(params) {
        d3.json(this.props.options.url.concat("?",$.param(params)),
            function(error, data){
                var options = this.props.options.params;
                if(data.date){
                    for(var i=0; i < data.result.length; i++){
                        data.result[i] = MG.convert.date(data.result[i], "x");
                    }
                }
                options.data = data.result;
                MG.data_graphic(options);
            }.bind(this));
    },

https://github.com/stitchfix/pyxleyJS/blob/master/src/metricsgraphics.js

I'll have to look in to it and try it with different date formatting.

sanjeevbhalla commented 8 years ago

Right. On this line data.result[i] = MG.convert.date(data.result[i], "x"); is called to convert Date. But because no format is passed it uses default format of %Y-%m-%d

https://github.com/mozilla/metrics-graphics/wiki/Convenience-Functions Converts a data object's values from strings to javascript Date objects that can then be plotted. The function parses %Y-%m-%d-styled timestamps by default. An optional third argument allows one to specify an alternative style, per the formats defined here: How do I as user of Pyxley override this behavior? (Sorry if I am missing something obvious - I am not a Javascript expert).

Thanks for your help.

On Thu, Jan 7, 2016 at 8:47 AM, Nick Kridler notifications@github.com wrote:

Here's the javascript code that does the conversion.

https://github.com/stitchfix/pyxleyJS/blob/master/src/metricsgraphics.js

— Reply to this email directly or view it on GitHub https://github.com/stitchfix/pyxley/issues/17#issuecomment-169723353.

markosole commented 6 years ago

This worked for me:

for(var i = 0; i < data.length; i++) {
                    data[i] = MG.convert.date(data[i], 'date', '%Y-%m-%dT%H:%M:%SZ'); 
 }

My date in database looks like this: 2018-04-28T13:24:55Z