ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Trouble with dates on x-axis
Posted by Pete D on Apr-25-2013 07:35
Attachments:
Hi Peter -

I'm sure you get this question a lot but I cannot seem to find the answer. I am trying to
create a simple XY Chart (in ruby) with dates on the x-axis. Here is my code:

require 'chartdirector'
require 'rubygems'
require 'active_support'

class Chart
  include ChartDirector
  attr_accessor :chart
  def initialize

    data_rows = {
      "first"  => [1, 2, 3, 5, 6, 7],
      "second" => [2, 4, 3, 4, 5, 3]
    }
    x_labels = 6.times.inject([]) {|l, i| l << (Time.now-10.days)+i.hours}

    c = ChartDirector::XYChart.new(450, 400, 0xdddddd, ChartDirector::Transparent, 3)
    title = c.addTitle("Chart Timings", "arialbd.ttf", 16)?
    title.setMargin2(0, 0, 6, 6)

    c.setPlotArea(10, 15,  300, 385, -1, -1, ChartDirector::Transparent, 0x000000)
    legendBox = c.addLegend2(c.getWidth / 2, title.getHeight, 5, "arialbd.ttf", 8)
    legendBox.setAlignment(ChartDirector::TopCenter)
    legendBox.setBackground(ChartDirector::Transparent, ChartDirector::Transparent)
    y_axis = c.yAxis
    y_axis.setTitle("Completion Time (ms)", "arialbd.ttf", 12)

    x_axis = c.xAxis
    x_axis.setDateScale3("{value|mm/dd hh:nn:ss}")
    x_axis.setLabelStyle("arial.ttf", 8, ChartDirector::TextColor, 45)

    layer = c.addLineLayer2
    layer.setXData(x_labels)
    puts x_labels.length
    data_rows.each do |name, data|
      layer.addDataSet(data, -1, name.setDataSymbol(ChartDirector::DiamondShape, 15)
      layer.setLineWidth(2)
    end
    c.layoutLegen
    c.packPlotArea(15, legendBox.getTopY + legendBox.getHeight + 10, c.getWidth - 20,
c.getHeight - 25)
    @chart = c.makeChart2(PNG)
  end
end
c = Chart.new
File.open("chart_timings.png", "wb") {|f| f.write c.chart}

You can see the results in the attached image. What I woud like to see is one date on the
x-axis per element in each data series.

Also, I woud like to include a vertical line at a specific date.

Your help is always appreciated.

Pete
chart_timings.png

  Re: Trouble with dates on x-axis
Posted by Peter Kwan on Apr-26-2013 00:04
Hi Pete,

If your data points are evenly spaced horizontally, and you would like to have the labels to be aligned with the points, instead of using setDateScale, you may use setLabels. The code is like:

x_axis = c.xAxis
x_axis.setLabels2(x_labels, "{value|mm/dd hh:nn:ss}")
x_axis.setLabelStyle("arial.ttf", 8, ChartDirector::TextColor, 45)

layer = c.addLineLayer2
layer.setLineWidth(2)
data_rows.each do |name, data|
   layer.addDataSet(data, -1, name.setDataSymbol(ChartDirector::DiamondShape, 15)
end

Note that no setXData or setDateScale is necessary.

If you use setXData and setDateScale, then the x-axis scale is treated similar to the y-axis scale. ChartDirector will choose the labels based on data range, but the labels may not be aligned with your data points. This method is usually used for unevenly spaced data points. (If you align the labels with evenly spaced data points, some labels can be very close together and overlap, and some labels can be very far apart, depending on how "unevenly spaced" your data points are.)

Most of the sample code included in ChartDirector uses the setLabels method (eg. the sample code "Simple Line Chart", "Multi-Line Chart", ...). A few examples use the setXData/setDateScale method (eg. the sample code "Missing Data Points" and "Uneven Data Points").

For the vertical line, you may use Axis.addMark. If you are using setLabels, the x-coordinate of the vertical line should be at the array index of the data points (that is, x=0, 1, 2, 3, ...). If you are using setXData/setDateScale, the x-coordinate is the x-coordinate of the data points.

The sample code "Marks and Zones (2)" demonstrates vertical and horizontal mark lines.

Hope this can help.

Regards
Peter Kwan