Sunday, March 27, 2011

In GDI+ how to draw a text vertically.

Two weeks ago , I moved to a new company, and become very busy, to be honest, at Ninemsn, the culture is quite relax, not too much pressure, while in my new company, cause the team is small, I need to do most of the design and development, so quite busy, that's why I didn't get time to update my blog. 

As my first assignment in this company is to develop a chart control, which need to support line chart, pie chart, stack bar chart etc.  At this point , some people might ask , why not just find one from the market?  well, that's a very good question, I ask them as well, but the answer is , their chart requirement is quite unique ,   for example, they need a kind of stack bar chart.  all the others are standard, while the width of the stack bar should represent the production, that means , if a company, their production is high,  the width of the bar should represent it.  to be honest, I can't find a control from the market can meet this requirement without any funky customization.  furthermore, their want the chart can be export to word natively, when I say natively  means, the chart can be edit in word, cause most chart control currently, they can be export to word as an image, that means the chart can't be editable in word.  based on these two reason , we decide to implement one chart control of our own. 

Technically, it is not difficult, but I do came across a few issues, and I resolve that. 
1. In GDI+ how to draw a text vertically. 
In GDI+ , the way to do it is rotate the axis, for example, if you want to rotate it 90 degree, then you can use RotateTransform function. 
Assume, you want to draw a text at point(100,100) vertically,here is how you gona to do that. 
e.Graphics.TranslateTransform(100,100);
e.Graphics.RotateTransform(-90);
e.Graphics.DrawString("Hello", new Font("Arial", 10f, FontStyle.Regular), Brushes.Red, 0, 0);
e.Graphics.ResetTransform();
The reason why I mention this is , most post missed one point, that you need to translate your axis first, before you do the rotate. cause by default, rotate transform is roate the axis base on Point(0,0), if you don't translate your axis to the position where you want to draw your string, then most of the time, you will not be able to find the string you just drew, cause it is out of range. 

2.In my case, the other challenge is how to draw on multiple platform(WPF, GDI+ and Word), I use an interface to abstract the underlying drawing functionality. 


No comments:

Post a Comment