axlsx icon indicating copy to clipboard operation
axlsx copied to clipboard

Line Chart X-Axis Data Points

Open ottodude125 opened this issue 13 years ago • 6 comments

I am creating a line chart with axlsx and I am unable to figure out how you can specify the data points to use from your sheet for the x axis. The chart defaults to starting the x axis to 0 and each data point increments that count by 1. I have included screen shots below of the output that I get vs the output that I would like to have.

Let me explain the scenario. I have a sheet which has week numbers listed. Each week there is a corresponding demand and capacity. With this we we can compare the capacity to demand on a week to week basis. If the chart were to be created today looking forward over the next year the left most x value would be week 40. It would continue incrementing to week 52 and then it would start over at 1 instead of incrementing anymore.

An example data set would be

screen shot 2013-10-04 at 5 37 44 pm

This is what I get when I create the line chart with axlsx:

screen shot 2013-10-04 at 5 26 29 pm

This is what I want to get when I create the line chart with axlsx. This chart is exactly the same except once I open axlsx's output in excel I update the data source for the chart so that the cells are defined where it should get the data for the x axis.

screen shot 2013-10-04 at 5 27 12 pm

An alternative way I can obtain the results I want is to create a scatter chart with axlsx and then when I open it in excel I change the chart type to a line chart. I dont have to update the data source for the chart in this case because in axlsx code the scatter chart allows you to specify the "xData" and "yData" whereas line chart only lets you specify the "data".

Thank You very much for any help!!!

ottodude125 avatar Oct 11 '13 17:10 ottodude125

Hy, I think I managed to do it this way

     sheet.add_chart(Axlsx::LineChart, :title => "Open") do |chart|
        chart.start_at 5, 5
        chart.end_at 25, 30
        chart.add_series :labels => sheet["A3:A9"], :color => "333333"
        chart.add_series :data => sheet["B3:B9"], :title => sheet["B2"], :color => "009900"
        chart.add_series :data => sheet["C3:C9"], :title => sheet["C2"], :color => "333399"
     end

rds13 avatar Nov 07 '13 15:11 rds13

@rds13 You Rock!! Thank you so much. I tweaked your code slightly but this worked. The :labels key is what I was missing.

My code is way to complex with equations to pull data dynamically from cells in other sheets so to reiterate what I did I showed your code below just in case anyone find this helpful down the road. The main difference is I eliminated your first "chart.add_series" since that adds an extra series in the key on the right of the graph. I used the :labels key in that definition in the other two "chart.add_series". The :labels key defines the data points that are used on the x axis and the :data key defines the y values.

Both my lines share the exact same x axis data points so this works. If you try this with multiple lines and each line has a different :labels definition the first add_series defined takes precedence over the others.

sheet.add_chart(Axlsx::LineChart, :title => "Design Capacity vs Demand") do |chart|
    chart.start_at 5, 5
    chart.end_at 25, 30
    chart.add_series :data => sheet["B3:B9"], :title => sheet["B2"], :labels => sheet["A3:A9"], :color => "009900"
    chart.add_series :data => sheet["C3:C9"], :title => sheet["C2"], :labels => sheet["A3:A9"], :color => "333399"
    chart.catAxis.title = 'Workweek'
    chart.valAxis.title = 'Designers'
end

ottodude125 avatar Nov 07 '13 17:11 ottodude125

Do you use Excel to visualize your graphs ? I can see my graphs with OpenOffice but Excel on Windows don't display my sheet and graphs.

rds13 avatar Nov 07 '13 17:11 rds13

I use Excel but I use two different versions. On my mac I use Excel 2011 and it works fine. On my windows desktop I use Excel 2007 and I experience bugs.

The charts will not show for me with the code up above in Excel 2007. I actually created Issue #260 because of this bug. I found that the ":color" key was causing the sheet to crash when I was opening the file. By deleting this key and letting the chart choose its own colors for the lines the page now loads without a problem.

ottodude125 avatar Nov 07 '13 17:11 ottodude125

Actually I removed the charts from my sheet and still experience error with Excel. I'll try to narrow to a specific instructions but it's a pain to debug.

rds13 avatar Nov 07 '13 22:11 rds13

I manage to remove error from my sheets with my modifications on merge request #263 . You might give it a try to get read of Excel corruption error messages.

rds13 avatar Nov 09 '13 17:11 rds13