ePlaice / For the Best Software on the Net

Mainly Free and Open Source Software


Software Dev't Navigation

Delphi Conversion | DataSets | DataGridView | Record Navigation | Data Formatting / Validation | Data Queries Data Input Form Data Readers MDI Forms Charts Using MySQL | MySQL NET Connector |

Valid XHTML 1.1

Latest news

27 Feb 2007: I decided to write a few lines about my experiences with C# and SharpDevelop, but got waylaid looking at some of my earlier experiences.

07 Mar 2007: SharpDevelop 2.1 Final has been released.

Links:

Charts Using .NET

SharpDevelop

One of the things I was looking for when I started out using .NET was a simple way of preparing charts both for a standalone Financial System that I have developed and also for use on the web.

Up until recently I have been using the excellent ZedGraph for my standalone applications, which just about handles all my requirements and integrates very nicely into my Financial package. I also wanted an easy method of displaying charts on the web; in particular I wanted to avoid any web processes that required a database to be maintained and loaded on the webserver. This simplified the task, since straightaway I eliminated anything to do with ASP, PHP or any other method that needed server side scripting. I just don't want the time and expense of having to set up a web database and learn all about ASP just so that I can show a chart on a webpage. A solution along the lines of Silverlight seemed ideal to me and I did see one or two applications for Silverlight 1.1 that might be suitable. I then got side-tracked looking at Google Chart API, but quickly became disillusioned by the time involved in setting up the data and the rather artificial constraints that were placed on the charts, plus the lengthy response time before displaying the chart on a web page. So I added another requirement, that the charts should be easy to build and take data from a wide range of data sources. The answer was a little surprising, but for me it seems an ideal solution - in fact the method involves using a database, Spreadsheet software, a XAML convertor and finally Silverlight 2.0

Silverlight Advantage

Of course it's quite easy to just create a chart in Excel and then create a JPG image of the chart which can then be displayed in HTML. However, this misses out the big advantage of the Silverlight chart which very easily allows the chart to be animated or key values displayed when you move the mouse over, or even have a chart that is interactive allowing change of scale, ability to zoom in etc. The charts below have not been developed any further in this way at the moment - but nevertheless they are so easy to build that I can find little reason not to use this solution.

MySQL For Chart Data

If you have some important data then the chances are that you will want to hold this on a database, because this provides a means of securing your data and also provides great flexibility. For example you might hold two different tables of values by date that you wish to combine into one chart. With SQL it's easy to join the tables and then you can use ZedGraph in an application or save the output in .csv format for loading to your favourite Spreadsheet. Having the chart data in a database is by no means mandatory; so for example you could maintain all the data in a spreadsheet instead of using a database.

Spreadsheet Charts

For quite a few years now I have been using OpenOffice for Spreadsheets and Word Processing, and although I haven't used Excel recently I suspect it's charting capability is still quite a bit more sophisticated than OO Calc. However, the good thing is that this doesn't matter too much - you can use whichever you have and you will still be able to produce nice looking web charts. Even though Calc comes with a chart wizard, I still found myself having to check various things on the Net because it is not that intuitive. However, once you have produced a few charts and learnt the language it all fits into place very readily. There are eight different chart types available as follows :- Column (vertical bars), Bar (horizontal bars), Pie, Area, Line, XY Scatter, Net, Stock and Column and Line. Some of these charts have 3D versions available. Most of the charts I see on the net are Bar, Line and Pie Charts with the occasional Area chart; and sometimes 3D on a 2D surface can make things look quite complex. There are all sorts of options regarding the Chart Title and position, together with labels. When setting up the Chart, there are options to choose the first row as label which is fine, since the label might be headed up 'Comp A price' and this is then used as the title for the label. Then you can choose the first Column as label which means that you want all the values in the first column to be headings for the x-axis. If you don't choose a column as x-axis labels then you get a default value of period 1 ... period n on your x-axis. Let's assume you produce a really nice looking chart and now want to display it on the web; in Excel all you need to do is select the chart and copy it to the clipboard and on to the next section. (For OpenOffice Calc it's a little bit more complicated because of the way OpenOffice copies to the Clipboard).

Workaround If Using OO Calc

I found that if I used a copy to Clipboard for a chart in OpenOffice, the chart did not produce a useable EMF (Extended Meta File) - in fact the axes appeared but the chart itself was not there. So the workaround is to save your file in Excel Format and then use the free Microsoft Excel Viewer to view the chart and to copy it to the clipboard here. I'm not sure why OO Calc can not perform this simple operation but maybe it's something to do with Microsoft charging for their software and Open Office being free. The next problem is that although the conversion from OO to Excel is very reasonable there are some minor annoyances such as the way dates are converted - Excel Viewer insists on spreading the date out over three lines - whereas my original has only two lines, so extra work involved in massaging the x-axis dates. Maybe, I should have just paid the money!!

Paste2Xaml

This is a nice application that you can find on the wpf-graphics website. The new version is particularly good as it is Silverlight 2.0 friendly and also allows you to enter names for any of the objects that interest you. So immediately you get all the XAML code for your chart for free, just by pasting the chart from Excel into the application area. When you paste in the chart, the screen rhs shows all the objects in the emf file. Then by double clicking on any object you can give it a meaningful name which can then be used in C# code as a handle for writing animation code etc. Of course, there is no necessity to write C# code you can just use the XAML code, import it into your development environment and reproduce the chart on the web as I have done below. I usually import the code into a SharpDevelop project generated from my template (see my WPF section for further details). When you press the export button you can switch between WPF and Silverlight XAML code, so you have good flexibility. The chart below was created using all the steps described in this section - so in actual fact I did not need to know anything about XAML at all. Now to have a go at some bar and pie charts.

I actually like this chart because it shows in a nutshell the stability that existed in the 1950's and 1960's, the turbulent 1970's and early 80's, the big hic-up in the early 90's and the benign environment that followed right up to 'credit crunch' of 2007/8. All the statistics have come from the ONS web site - which is a mine of information. Currently, it looks like blind panic has set in as the recession starts to take hold with the economy lurching from inflation to deflation within a few months. Wild swings such as these are destabilizing and have already dramatically affected sterling. In more normal times devaluations have the affect of ramping up inflation which provides significant debt forgiveness, but what is really causing the panic in Government circles is that there will be at least a short period of falling prices and maybe longer if all trading blocks decide to devalue at the same time. This would be catastrophic because instead of all those outstanding mortgages becoming cheaper, they will actually cost more in real terms. So we won't join the euro because we would not be able to play the competitive devaluation game. What makes it difficult this time is that the USA is already down to 1% interest rates - difficult to beat that!!! Perhaps there is another way - tackle the debt mountain (which is at the heart of the problem) by encouraging debt repayment instead of more personal credit and government borrowing so that people start to live within their means.

Bar Chart Example

This example shows the headcount during the last quarter of 2007 in the UK Civil Service. The number totals 474,820 plus another 21,500 for the Scottish Executive and Welsh Assembly. The chart was built in OpenOffice transferred to Excel without any changes :-

Pie Chart Example

The data has been taken from the ONS website and shows the distribution of employment in the UK for the fourth quarter of 2007. This shows what a 'Housing' economy looks like with a heavy preponderance on Services, Banking, Public Admin, Construction and Distribution. Notice the relatively small size of Manufacturing and Agriculture which does not augur well for an export led 'bounce back' resulting from massive devaluation of sterling ongoing in 2008. The figures represent thousands in employment for each industry. It will be interesting to see what this looked like ten years ago, before the advent of the 'Service' (Housing economy). For a Housing economy a massive Construction industry is required to build all the apartments (used to be called flats), an oversized Banking industry to grant all those mortgages as a sideline with gambling as their main day job, a massive Services and Public Admin Industry to look after all these flat occupiers and finally a large Distribution Industry so that the flat occupiers can spend their gains (notional) from ever rising House prices on cheap imported food and goods. Meanwhile, the few remaining manufacturers have been hit hard by cheap imports from the World's sweat shops and land for farms, gardens, school playing fields have been turned into flats. Meanwhile at the last count there were 850,000 empty homes in the UK and enough empty Commercial property for another 400,000 homes - so what are we to make of Government targets to build another 3 million homes? Note - I was looking for a good 3D PieChart and so I checked out the Visifire offering. This seems to work quite well apart from the size of the XAP file (231Kb as opposed to about 6Kb using the Excel cut and paste method) which seems to be a common problem with most libraries. However, to achieve similar effects using the Excel method would need a considerable amount of coding.

A few nice features with this chart - when the chart is first displayed it gives a little wobble about the axis. When you move the mouse over a slice it displays the name, quantity and percentage. If you click on a slice it drags the slice out and a further click moves the slice back to its original position. I've had a look at the Microsoft Data Visualization charts which do not have so many features (apparently) and definitely not so flashy. However, it's still early days for Microsoft and I'm sure the next release will have even more features.