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

Message ListMessage List     Post MessagePost Message

  2 Labels for one x-axis
Posted by Sherri on Apr-04-2016 17:05
Attachments:
Hi,

I would like to create a graph as attached. The y-axis will be the amount and the x-axis will have 2 labels, one is by duration and another one by type (A & B). Each color represents different data set. Do you know how to write the code for this chart in python?

Thank you
Graph.PNG

  Re: 2 Labels for one x-axis
Posted by Peter Kwan on Apr-05-2016 04:07
Attachments:
Hi Sherri,

There are many methods. The method below is by using two x-axis for the 2 rows of labels. The code is the same as the "Glass Multi-Bar Chart: sample code except that two x-axes are used for the 2 layer of labels:


#!/usr/bin/python
from pychartdir import *

# The data for the bar chart
data0 = [100, 125, 245, 147, 67]
data1 = [85, 156, 179, 211, 123]
data2 = [97, 87, 56, 267, 157]
labels = ["Mon", "Tue", "Wed", "Thur", "Fri"]
labels2 = ["AAA", "BBB", "BBB", "CCC", "CCC"]

# Create a XYChart object of size 540 x 375 pixels
c = XYChart(540, 400)

# Add a title to the chart using 18pt Times Bold Italic font
c.addTitle("Average Weekly Network Load", "timesbi.ttf", 18)

# Set the plotarea at (50, 55) and of 440 x 280 pixels in size. Use a vertical gradient color from
# light blue (f9f9ff) to blue (6666ff) as background. Set border and grid lines to white (ffffff).
c.setPlotArea(50, 55, 440, 280, c.linearGradientColor(0, 55, 0, 335, 0xf9f9ff, 0x6666ff), -1,
    0xffffff, 0xffffff)

# Add a legend box at (50, 28) using horizontal layout. Use 10pt Arial Bold as font, with
# transparent background.
c.addLegend(50, 28, 0, "arialbd.ttf", 10).setBackground(Transparent)

# The tick lengths of the major and minor ticks
c.xAxis().setTickLength(45, 20)

# Use minor ticks if the second label labels remain the same
for i in range(0, len(labels) - 1) :
if labels2[i] == labels2[i + 1] :
labels[i] = "-" + labels[i]
c.xAxis().setLabels(labels)

# Use another x-axis for the second layer labels
xAxis2 = c.addAxis(Bottom, 25)
xAxis2.syncAxis(c.xAxis())
xAxis2.setColors(Transparent)
xAxis2.setLabelStyle("arialbd.ttf", 10)

# For consecutive second layer labels of the same text, we put only one label in the middle
startPos = 0
for i in range(0, len(labels2) + 1) :
if i == len(labels2) or labels2[startPos] != labels2[i] :
xAxis2.addLabel((startPos + i - 1) / 2.0, labels2[startPos])
startPos = i


# Draw the ticks between label positions (instead of at label positions)
c.xAxis().setTickOffset(0.5)

# Set axis label style to 8pt Arial Bold
c.xAxis().setLabelStyle("arialbd.ttf", 8)
c.yAxis().setLabelStyle("arialbd.ttf", 8)

# Set axis line width to 2 pixels
c.xAxis().setWidth(2)
c.yAxis().setWidth(2)

# Add axis title
c.yAxis().setTitle("Throughput (MBytes Per Hour)")

# Add a multi-bar layer with 3 data sets
layer = c.addBarLayer2(Side)
layer.addDataSet(data0, 0xff0000, "Server #1")
layer.addDataSet(data1, 0x00ff00, "Server #2")
layer.addDataSet(data2, 0xff8800, "Server #3")

# Set bar border to transparent. Use glass lighting effect with light direction from left.
layer.setBorderColor(Transparent, glassEffect(NormalGlare, Left))

# Configure the bars within a group to touch each others (no gap)
layer.setBarGap(0.2, TouchBar)

# Output the chart
c.makeChart("glassmultibar.png")


Hope this can help.

Regards
Peter Kwan
glassmultibar.png

  Re: 2 Labels for one x-axis
Posted by Sherri on Apr-22-2016 10:55
Hi Peter,

The approach is nice. If I am to twist labels2 to labels2 = [AAA,BBB,CCC] with known that Tue and Wed are under BBB and Thur and Fri are under CCC, how can I set the tick as shown in your graph?

Is Axis.setTickLength() command apply the same tick length in the whole axis? Is there a way to change the tick length according to preference? Thank you.

  Re: 2 Labels for one x-axis
Posted by Peter Kwan on Apr-22-2016 18:01
Hi Sherri,

For the labels ["Mon", "Tue", "Wed", "Thur", "Fri"], if a label starts with "-", then it is associated with a minor tick, So "Wed" will be associated with a major tick (long tick in the code), and "-Wed" will be associated with a minor tick. In both case, the label will be displayed as "Wed". See:

http://www.advsofteng.com/doc/cdpython.htm#Axis.setLabels.htm

In my original code, the code determines if a label should be associated with a minor tick by checking if the second row of labels has not changed. If it should be a minor tick, it adds "-" in front of the label. The code is:

for i in range(0, len(labels) - 1) :
    if labels2[i] == labels2[i + 1] :
        labels[i] = "-" + labels[i]

For your case, you mentioned your code knows how the labels are grouped, so it should be able to determine which labels require adding "-" in front.

Hope this can help.

Regards
Peter Kwan