DougKruger
DougKruger

Reputation: 4624

How to set nvd3 lineChart x-axis to use string

I'm a complete noob, I wish to plot a lineChart using the python wrapper of the nvd3 library built on top of d3.js. I've been exploring the x_axis_format of the lineChart and desire to utilize custom formatting so I can use strings on x-axis . The NVD3 base class from the documentation states create_x_axis(name, label=None, format=None, date=False, custom_format=False), and from NVD3/lineChart.py:

if kwargs.get('x_is_date', False):
            self.set_date_flag(True)
            self.create_x_axis('xAxis',
                               format=kwargs.get('x_axis_format', '%d %b %Y'),
                               date=True)
            self.set_custom_tooltip_flag(True)
else:
    if kwargs.get('x_axis_format') == 'AM_PM':
          self.x_axis_format = format = 'AM_PM'
    else:
       format = kwargs.get('x_axis_format', 'r')
       self.create_x_axis('xAxis', format=format,
       custom_format=kwargs.get('x_custom_format', False))

But when I do this:

lineChart(name="lineChart-With-Interactive-Guideline",height=400, width=800, x_is_date=False, custom_format=False, use_interactive_guideline=True)

I get an empty page, no chart. Can someone please show me how to set my x-axis to allow string values such as "2016-07-01", "2016-07-02"?

Upvotes: 1

Views: 340

Answers (1)

IQR
IQR

Reputation: 1

You must use lineChart x_axis_format argument with a format string, i.e.: x_axis_format='%d/%b/%Y'

After that, you need to set the chart.add_serie x value to a list of timestamps, as the timestamp is not in Unix Epoch, multiply them by 1000:

xdata = [(datetime.timestamp(x+timedelta(days=1)))*1000 for x in df["date"]]

chart.add_serie( x=xdata )

Upvotes: 0

Related Questions