|
can't encode from unicode to utf-8 |
Posted by Alexei on May-24-2013 04:14 |
|
Good evening!
I use Python 2.7.3 and ChartDirector 5.1. I need to display cyrillic symbols. On Windows
XP everything works good (see Windows.png), but on Linux (i386/i686) platform I have a
problem (see Linux1.png).
Code:
# -*- coding: utf-8 -*-
from pychartdir import *
# check
import locale
print locale.getdefaultlocale()
print locale.getlocale()
print locale.getpreferredencoding()
# data and labels
data1 = [10, 7, 5, 3, 1, 4, 1, 9, 0, 11, 7, 2]
data2 = [6, 9, 3, 0, 4, 2, 3, 1, 8, 3, 5, 4]
labels = [u'январь', u'февраль', u'март', u'апрель', u'май', u'июнь', u'июль', u'август',
u'сентябрь', u'октябрь', u'ноябрь', u'декабрь']
# chart params
chart = XYChart(1200, 700)
chart.addTitle(u'Количество сбоев по месяцам за период 2010-2011гг.')
chart.setPlotArea(-5, 30, 1205, 610)
chart.addLegend(550, 660, 0, '', 8).setBackground(Transparent)
chart.xAxis().setLabels(labels)
# adding data
layer = chart.addBarLayer2(Side, 3)
textBox = layer.setDataLabelStyle()
textBox.setPos(textBox.getLeftX(), textBox.getTopY() - 20)
layer.setBarGap(0.4, 0)
layer.addDataSet(data1, 0xff8080, u'2010г.')
layer.addDataSet(data2, 0x80ff80, u'2011г.')
chart.makeChart('fails.png')
If I delete 1st row '# -*- coding: utf-8 -*-', then... (see Linux2.png)
Usage of encode() method don't helps me (for example: u'январь'.encode('utf-8') or
unicode(u'январь').encode('utf-8')
On Linux platform block of rows № 5-9 prints:
('ru_RU', 'KOI8-R')
(None, None)
KOI8-R
On Windows:
('ru_RU', 'cp1251')
(None, None)
cp1251
Thanks.
|
Re: can't encode from unicode to utf-8 |
Posted by Peter Kwan on May-24-2013 18:52 |
|
Hi Alexei,
The Linux1.png is the correct chart. The reason you cannot see the text is probably because there is no cyrillic font in your computer for ChartDirector to use.
In your code, the default font is used, which is Arial. The Arial font comes with Windows, and the Arial font contains cyrillic characters, so you can see the characters. On Linux, there is no Arial font, so a "substitute" font is used, and it does not have cyrillic characters.
To solve the problem, please download the Arial font and put it in the "fonts" subdirectory under the directory that contains "libchartdir.so". It is possible to download the Arial font legally and free of charge. See:
http://www.advsofteng.com/doc/cdphp.htm#fontspec.htm
Alternatively, you may download other font that have cyrillic characters, and save them in the "fonts" subdirectory, and configure the chart to use that font.
Hope this can help.
Regards
Peter Kwan |
Re: can't encode from unicode to utf-8 |
Posted by Alexei on May-28-2013 01:19 |
|
Hi, Peter!
Thank you very much for your help. It works.
But I have another problem How can I display zero values? On attached pictures zero
values is not displayed. |
Re: can't encode from unicode to utf-8 |
Posted by Peter Kwan on May-28-2013 17:32 |
|
Hi Alexei,
The setDataLabelStyle are supposed to display labels inside the bars. If the bar is too short (eg. zero in length) to contain the label, by default, the label will not be displayed.
To solve the problem, please change:
textBox = layer.setDataLabelStyle()
textBox.setPos(textBox.getLeftX(), textBox.getTopY() - 20)
to:
textBox = layer.setAggregateLabelStyle()
The setAggregateLabelStyle is for displaying labels outside of the bar (on top of the bar for positive vertical bars).
Hope this can help.
Regards
Peter Kwan |
Re: can't encode from unicode to utf-8 |
Posted by Alexei on May-29-2013 03:58 |
|
Hi, Peter!
Thanks a lot! |
|