Difference between revisions of "TAChart Tutorial: Stacked BarSeries"

From Lazarus wiki
Jump to navigationJump to search
(Introduction)
 
(Setting up the chart)
Line 3: Line 3:
  
 
= Introduction =
 
= Introduction =
In the tutorial [[TAChart_Tutorial:_BarSeries|TAChart Tutorial: BarSeries]] we had demonstrated how to create a chart with bar series located side-by-side. Stacked bars are another popular variant of bar charts where the grouped data are drawn stacked onto each other. In this way, the individual contributions and their sum can be shown within the same plot.  
+
In the tutorial [[TAChart_Tutorial:_BarSeries|TAChart Tutorial: BarSeries]] we had demonstrated how to create a chart with bar series located side-by-side. Stacked bars are another popular variant of bar charts where the grouped data are drawn stacked onto each other. In this way, the individual contributions and their sum can be shown within the same plot. There is also a variant in which the total bar heights are normalized to 100% to emphasize the relative contributions.
  
 
We'll apply this technique in this tutorial to plot the progress in generation of renewable energies during the last years.
 
We'll apply this technique in this tutorial to plot the progress in generation of renewable energies during the last years.
  
 
As usual, you must have some basic knowledge of Lazarus, Object Pascal, and the key concepts of TAChart to follow the tutorial. If you have never worked with bar charts it is a good idea to look at the related tutorial explaining the [[TAChart_Tutorial:_BarSeries|side-by-side arrangement of bars]].
 
As usual, you must have some basic knowledge of Lazarus, Object Pascal, and the key concepts of TAChart to follow the tutorial. If you have never worked with bar charts it is a good idea to look at the related tutorial explaining the [[TAChart_Tutorial:_BarSeries|side-by-side arrangement of bars]].
 +
 +
= Preparations =
 +
== Setting up the chart ==
 +
Create a new project. Add a TAChart component, and align it alClient to completely fill the form. Maybe you should increase the size of the form a bit, but you can do this later when you see the data.
 +
 +
== Data ==
 +
As mentioned in the introduction we will plot some "real data" today. But since some tweaking of the chart will be necessary again and again it is maybe a good idea to come back to the other bar series tutorial and use a RandomChartSource for temporary data. This allows us to optimize the appearence of the chart without having to recompile the project again and again. At the end, we will remove the RandomChartSource and add the data of interest.
 +
 +
There is a big difference to the side-by-side bar chart: In that layout you had to use a series for each bar type. The stacked bar chart is completely different: it contains only a single series, but gets the different bars from multiple y values of the chart source.
 +
 +
What's that?
 +
 +
The data items for each bar are TChartDataItem records declared in unit TACustomSource:
 +
<source>
 +
type
 +
  TChartDataItem = packed record
 +
  public
 +
    X, Y: Double;
 +
    Color: TChartColor;
 +
    Text: String;
 +
    YList: TDoubleDynArray;
 +
    ...
 +
  end;
 +
</code>
  
  

Revision as of 22:56, 21 March 2014

WORK IN PROGRESS NOT FINISHED

Introduction

In the tutorial TAChart Tutorial: BarSeries we had demonstrated how to create a chart with bar series located side-by-side. Stacked bars are another popular variant of bar charts where the grouped data are drawn stacked onto each other. In this way, the individual contributions and their sum can be shown within the same plot. There is also a variant in which the total bar heights are normalized to 100% to emphasize the relative contributions.

We'll apply this technique in this tutorial to plot the progress in generation of renewable energies during the last years.

As usual, you must have some basic knowledge of Lazarus, Object Pascal, and the key concepts of TAChart to follow the tutorial. If you have never worked with bar charts it is a good idea to look at the related tutorial explaining the side-by-side arrangement of bars.

Preparations

Setting up the chart

Create a new project. Add a TAChart component, and align it alClient to completely fill the form. Maybe you should increase the size of the form a bit, but you can do this later when you see the data.

Data

As mentioned in the introduction we will plot some "real data" today. But since some tweaking of the chart will be necessary again and again it is maybe a good idea to come back to the other bar series tutorial and use a RandomChartSource for temporary data. This allows us to optimize the appearence of the chart without having to recompile the project again and again. At the end, we will remove the RandomChartSource and add the data of interest.

There is a big difference to the side-by-side bar chart: In that layout you had to use a series for each bar type. The stacked bar chart is completely different: it contains only a single series, but gets the different bars from multiple y values of the chart source.

What's that?

The data items for each bar are TChartDataItem records declared in unit TACustomSource: <source> type

 TChartDataItem = packed record
 public
   X, Y: Double;
   Color: TChartColor;
   Text: String;
   YList: TDoubleDynArray;
   ...
 end;