Do you want to know how your customers use your applications and devices? Try Qt Insight.
最新版Qt 6.3已正式发布。 了解更多。
最新バージョンQt 6.3がご利用いただけます。 詳細はこちら

Simple QtScript-based bar chart

It is just bound to happen, isn't it? Since I share the office with Kent, our QtScript guru, it is indeed a matter of time before a QtScript-based Graphics Dojo example falling from the sky. Here it is: a bar chart.

There are many ways to create your bar chart. You can use an office suite like You can use the web-based API, such as Google Chart. You can also use insert-your-favorite-tool-here. What is shown below is the QtScript-based solution. Due to the nature of a scripting language, the data for the chart is actually inside the script, or rather it is one important piece of the script itself. Thus, creating a new chart is a matter of editing or adding new chart data and relaunch it. This eliminates the need for a routine that parses the input. Quite a time saver, isn't it? Combined with the lack of compiling step, resistance is futile.


As an example, for the chart above (which is just the SquirrelFish benchmark result redone with the tool), here is the relevant script fragment:

chart.windowTitle = "SquirrelFish Performance";
chart.suffix = "runs"; = {
  "WebKit 3.0" : 5.4,
  "WebKit 3.1" : 18.8,
  "SquirrelFish" : 29.9,
"SquirrelFish Extreme" : 63.6 };

The colors for each bar are generated automatically, see Helder's QQ 26 Adaptive Coloring for Syntax Highlighting for the general idea. If you do not favor the automatically picked colors, use the mouse wheel to change them (at once). Resizing the window allows you to find the optimum overall dimension for your chart. And once you are satisfied with the result, mouse click to save it to an image file.

The code is available from our brand new git repository for Graphics Dojo under the subdirectory barchart. Here is a way to get it:

git clone git://

To run the script, you need the bindings. Just grab that QtScript binding generator, build and run it (see its README), and the use the built qs_eval like:

/path/to/qtbindings/qs_eval/qs_eval barchart.qs

Finally, if you want to creat a lot of bar charts, just split out the actual chart instantiation (line 122 onwards) into its own script file. This way, you can create a new chart with touching barchart.qs very often.

PS: I thank Kent for his help which made my first QtScript endeavor as smooth as it could be.

Blog Topics: