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

Message ListMessage List     Post MessagePost Message

  error when run setLabels()
Posted by pts on Jun-17-2009 21:48
Attachments:
i use chartdirector for python version, and i use xlrd to read data from excel 2003, my code:
###########################
# coding=utf-8
from pychartdir import *
import xlrd

def readXls(file, col_data):
#     data struct example:
#     data = {}
# data['郑州'] = {'0501 - 0506':20, '0601 - 0606':30, '0701 - 0706':35}
# data['洛阳'] = {'0501 - 0506':23, '0601 - 0606':33, '0701 - 0706':33}
# data['信阳'] = {'0501 - 0506':27, '0601 - 0606':37, '0701 - 0706':37}
# data['安阳'] = {'0501 - 0506':21, '0601 - 0606':31, '0701 - 0706':31}
col_date = 0 #col of 周期
col_name = 3 #col of 局名

xls = xlrd.open_workbook(file)
sheet = xls.sheet_by_name(u"汇总")
nrows = sheet.nrows
data = {}
for row in range(1, nrows):
val_name = sheet.cell_value(row, col_name)
val_date = sheet.cell_value(row, col_date)
val_data   = sheet.cell_value(row, col_data)

data.setdefault(val_name, {})
data[val_name][val_date] = val_data
return data

def makePNG(data):
labels = list(data.iterkeys())
legend_labels = sorted(list(data[labels[0]].iterkeys()))
## labels = [x.encode("gbk") for x in labels]

baseColor = 0xff8080
c = XYChart(600,400)
c.setDefaultFonts('mingliu.ttc')
c.addTitle('汇总', "simhei.ttf", 12)
c.setPlotArea(50, 25, 550, 350, 0xffffc0, 0xffffe0)
c.addLegend(70, 18, 0, "", 8).setBackground(Transparent)
c.yAxis().setTitle('处理时间<24(百分比)', 'simhei.ttf')
c.yAxis().setTopMargin(20)
c.xAxis().setLabels(labels)
c.xAxis().setLabelStyle('',10)
layer = c.addBarLayer2(Side, 3)
for i in legend_labels:
datai = [x[i] for x in data.itervalues()]
layer.addDataSet(datai, baseColor, i.encode("gbk"))
baseColor += 25600
c.makeChart("huizong.png")

if __name__ == "__main__":
file = 'huizong.xls'
data = readXls(file, 7)
makePNG(data)
##########################

my excel file is alse upload.
when i run ,it shows:
>>>
Traceback (most recent call last):
  File "<string>", line 244, in run_nodebug
  File "E:\\temp\\zhuanyun_time\\makePNG.py", line 53, in <module>
    makePNG(data)
  File "E:\\temp\\zhuanyun_time\\makePNG.py", line 41, in makePNG
    c.xAxis().setLabels(labels)
  File "D:\\Python25\\lib\\site-packages\\pychartdir\\pychartdir.py", line 724, in setLabels
    return TextBox(_r("Axis.setLabels", self.this, labels))
TypeError: Error converting argument 1 to type class StringArray
>>>

can somobody help me? thanks.
huizong.xls
huizong.xls

42.00 Kb

  Re: error when run setLabels()
Posted by Peter Kwan on Jun-18-2009 01:45
Hi pts,

The error message means that labels are not an array of text strings. In ChartDirector for Python, the only text strings that are supported are values of type "str" (that is, type(myTextString) == type("") should be true). Other data types are not supported as text strings.

I have not used xlrd before. I suspect in your case, the data.iterkeys() may not be Python "str" objects, but are Python "unicode" objects. May be you can try to convert them to "str" objects using UTF8 encoding.

labels = list(data.iterkeys())
for i in range(len(labels)) :
     labels[i] = labels[i].encode("utf-8")

Also, for your other text strings, please use "utf-8" (not "gbk"). ChartDirector currently only supports "utf-8" encoding. The "utf-8" encoding is a kind of unicode encoding and can represent all possible unicode characters, including all characters in "gbk".

Hope this can help.

Regards
Peter Kwan

  Re: error when run setLabels()
Posted by pts on Jun-18-2009 12:46
yes,i have use:
labels = [x.encode("gbk") for x in labels]
but the xlabel show some irrecognizable  code.

thank you very very much.