You are not logged in.

1

Monday, September 12th 2005, 2:22am

How to create a realtime 2D bar graph with axises and legend within the chart ?

Hi guys,

Currently, im working on the statistics of my company's application which it requires me to generate the data in a form of 2D bar graph.

May i know how to create a 2D bar graph with x-axis and y-axis plus a simple legend to indicate the differences of those bars? Preferably, sample codes would do. :)

Thanks.

2

Monday, September 12th 2005, 3:30am

I don't think there are any pre-canned graphing classes (seems like it might be a good idea), but maybe there are some 3rd party classes out there for it. To build it directly in Qt you might have to use the QPainter class and use methods of that such as drawPolygon and drawText.
-- "Quality is free", quoted from Dr. W. E. Deming

3

Monday, September 12th 2005, 3:34am

Thanks virtualbrainsucker, i try it first with QPainter class, it seems tedious to draw out the graph...anyway, i try to do it...

anyway, is there any samples codes that i can refer ? thanks.

4

Monday, September 12th 2005, 8:46am

RE: How to create a realtime 2D bar graph with axises and legend within the chart ?

If the licensing policy of you application permits it take a look at QWT(.sourceforge.net)
There's no place like ::1

5

Monday, September 12th 2005, 9:49am

Thanks expert. I think i dont have the license policy for Qwt. Pls advise how to use the QPainter and Qcanvas for drawing the bar graph with axises and legends.

Thanks.

6

Tuesday, September 13th 2005, 2:36am

Quoted

Originally posted by wkk1000
Thanks expert. I think i dont have the license policy for Qwt. Pls advise how to use the QPainter and Qcanvas for drawing the bar graph with axises and legends.

Thanks.


Are you using Qt 3? The answer is a bit different with Qt 4.

I think if you're GPL you're ok, but if commercial, you'd need to think about licensing for that. On the other hand, simple bar charts are not hard, they're basically just rectangles and lines, with some labels. If you've never done graphical programming you have some learning to do, but don't fear it, as it isn't hard.

The Qt assistant help page shows something really simple:

Source code

1
2
3
4
5
6
    void SimpleExampleWidget::paintEvent()
    {
        QPainter paint( this );
        paint.setPen( Qt::blue );
        paint.drawText( rect(), AlignCenter, "The Text" );
    }


You'll simply derive from QWidget, and override the paintEvent.
-- "Quality is free", quoted from Dr. W. E. Deming

7

Tuesday, September 13th 2005, 4:33am

Thanks for ur comments , Chief Software Engineer... as u said drawing the simple bars graph is not difficult, how about determining the scale factor of the axis-y versus axis-x and draw those axises within the bar graph...?

and how to create those grid lines as the background to the bars graph? thanks experts....L:P

8

Tuesday, September 13th 2005, 8:13am

Quoted

Originally posted by wkk1000
Thanks expert. I think i dont have the license policy for Qwt.


Are you sure ?

Qwt licence even allows to link statically in closed source applications. All you have to do is
to mention its usage.

Uwe

  • "AlexKiriukha" is male

Posts: 1

Location: Ukraine

  • Send private message

9

Tuesday, September 13th 2005, 9:04am

Quoted


Are you using Qt 3? The answer is a bit different with Qt 4.

Do you mean using Model/View Architecture?
[Fedora Core 4]

10

Wednesday, September 14th 2005, 4:00am

Quoted

Originally posted by AlexKiriukha

Quoted


Are you using Qt 3? The answer is a bit different with Qt 4.

Do you mean using Model/View Architecture?


That's part of it for some uses, but for graphics I believe some of what was available in Qt 3 is no longer available in Qt 4. See:
http://doc.trolltech.com/4.0/qt4-intro.html

In particular:
http://doc.trolltech.com/4.0/qt4-arthur.html

I don't believe QCanvas is available in version 4. Version 4 antialiasing though is very nice (though a bit slow).

As for automated scaling, that's something you'd have to write. The math isn't hard for that, and there are numerous ways to apply scaling.
-- "Quality is free", quoted from Dr. W. E. Deming

11

Thursday, September 15th 2005, 1:41am

Hi there,

For your info, im not using MVC model in my project...And I really dont want to depend on certain components like Qwt to draw my bar graph...all i need is a simple bar graph using basic classes from QT istself...thanks for ur help :).

12

Thursday, September 15th 2005, 2:11am

It still differs somewhat for Qt 3 versus Qt 4, assuming you will be drawing your own graphs with primitives. Qt 4 antialiasing is quite nice. Also have a look at QPicture, used to replay QPainter commands. If you need speed, you'll probably have to use a QGLWidget and render with OpenGL, as the QPainter would not use hardware acceleration (using OpenGL can complicate things, but it's incredibly fast in comparison to software rendering, especially with antialiasing).
-- "Quality is free", quoted from Dr. W. E. Deming

13

Thursday, September 15th 2005, 10:11am

Quoted

Originally posted by wkk1000
...all i need is a simple bar graph using basic classes from QT istself...thanks for ur help :).


All you have is QPaintDevice/QPainter.

Uwe

14

Friday, September 16th 2005, 2:00am

Hi Uwe, all those contributed to his thread,

Thanks for all your info. Now, im trying to experiment using the QCanvas class . There's another problem where y-axis needs to be scalable enough to stretch itself to correspond with the highest data values of a bar.. How shall i do that ?

Thanks. Plus,how shall i draw the legend of the graph?

ls

Beginner

  • "ls" is male

Posts: 12

Location: Portugal

  • Send private message

15

Friday, September 16th 2005, 11:06am

Quoted

... y-axis needs to be scalable enough to stretch itself to correspond with the highest data values of a bar.. How shall i do that ?
quote]

Well, you can use logarithmic aproachs...
Lets say you would like y_axis to be 1, 2, 5, 10, 20, 50, 100 or other number kinda those...

First you should get number base 10 power,
p=(int)log10(your_max_y);

You can now now set
max_y_axis=pow(10,p);

But you must check the limits:
if (2*pow(10,p)>=your_max_y) max_y_axis=2*pow(2,p);
if (5*pow(10,p)>=your_max_y) max_y_axis=5*pow(5,p);
if (10*pow(10,p)>=your_max_y) max_y_axis=10*pow(10,p);

Now you got a "round" axis y max value close but greater then (or equal to) your max y value.

That's just a suggestion, of course!

16

Monday, September 19th 2005, 9:42am

Thanks for ur examples, ls...will let u know the result, by the way, how about drawing the legend for a particular abr graph?

Thanks.