Recent

Author Topic: Transparent series  (Read 6763 times)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Transparent series
« on: July 15, 2018, 08:26:01 pm »
Is it possible to add transparency to line series?

For a scientific project, I would like to draw a chart similar to the ones shown at http://andrewgelman.com/wp-content/uploads/2012/08/Rplot-with-fading-col-copy.jpg or http://cloud.originlab.com/www/resources/graph_gallery/images_galleries_new/Error_Bars_with_Fill_Area.png, but I don't know how to accomplish this with TAChart. If possible, I would prefer a solution based on Lazarus default box content without needing any external libraries or packages.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Transparent series
« Reply #1 on: July 15, 2018, 08:35:01 pm »
I don't know how to accomplish this with TAChart. If possible, I would prefer a solution based on Lazarus default box content without needing any external libraries or packages.

I've never used TAChart. Is it possible to do it in Excel? Would that be a possible solution if TAChart can't do it?

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Transparent series
« Reply #2 on: July 15, 2018, 09:50:35 pm »
You can use normal TCanvas to draw.
But it doesn't handle transparency by default, as well doesn't handle antialias (at least on Windows).

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #3 on: July 15, 2018, 10:25:12 pm »
It's already there: Every series has a property "Transparency". Its default is 0 = opaque, maximum is 255 = fully transparent. Set some intermediate value to see the background shining through. There are some issues here and there, though. But alternatively you can also draw the chart with the BGRABitmapDrawer or OpenGLDrawer which fully support transparency.

The easiest way to use BGRABitmap for rendering a chart is to link a TChartGUIConnectorBGRA to the property GUIConnector of the chart. Then the entire chart is painting using the BGRABitmap routines. This requires that you have once compiled the package BGRABitmapPack.lpk (available via OPM), and installed the package tachartbgra.lpk (in the TAChart directory).

An example with a transparent bar series and additional renderung with OpenGL is shown in the demo project "opengl" in the "demo" folder of the TAChart installation.

See also:
http://wiki.lazarus.freepascal.org/TAChart_documentation#Transparency
http://wiki.lazarus.freepascal.org/TAChart_documentation#BGRABitmap_drawer

Note however that error bars are not yet available - they are on my list, but I did not find the time to implement them.
« Last Edit: July 15, 2018, 10:28:50 pm by wp »

lainz

  • Hero Member
  • *****
  • Posts: 4460
    • https://lainz.github.io/
Re: Transparent series
« Reply #4 on: July 15, 2018, 10:32:43 pm »
That's really good wp!

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #5 on: July 16, 2018, 12:13:38 am »
A shaded error bar plot as mentioned in above OriginLab reference can be created by a combination of a lineseries and a stacked area series: the line series contains the "normal" data values", and the area series takes care of the error limits. The area series must contain two y values. The first y value is the lower error limit, and the second y value is the difference between upper and lower error value - when these two values are stacked on top of each other the error band is clearly visible. Since the area series always fills the area between the curve and the zero level the area underneath the lower error limit must be drawn with a clear brush. Variation of brushes between stacks can be accomplished using the TChartStyles component which must be assigned to the Styles property of the stacked series.

The attached project shows the necessary code. While everything can be done in the object inspector I decided to do it in code to show the essential steps - study the method "CreateErrorSeries".

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #6 on: July 16, 2018, 10:12:41 am »
And here's the same with BSpline series.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Transparent series
« Reply #7 on: July 16, 2018, 07:57:44 pm »
Thanks to all of you, especially @wp. Your comments and examples are very helpful.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Transparent series
« Reply #8 on: July 16, 2018, 08:08:03 pm »
Unfortunately, the examples don't seem to work with the Mac. Is there any trick to enable transparency on macOS?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #9 on: July 16, 2018, 08:23:30 pm »
Just to detail my plans:

I am thinking of adding an ErrorBarX and ErrorBarY property to most series defining the size of the x and y errors, either constant, or a percentage of the data value, or an individual value taken from the chart source; positive and negative errors will be allowed to be different. These propertes definitely will be used in TLineSeries, TPolarSeries, TCubicSplineSeries, TBSplineSeries and TFitSeries, maybe also in TAreaSeries and even in TBarSeries, to draw the usual error bar(s), vertical and/or horizontal.

In the FitSeries, I have extended fitting routines which use the error bar values (y only) to calculate statistics: standard errors of fitting parameters, confidence limits etc. Confidence/prediction bands will be drawn by the fit series similar to the banded series above like shaded areas.

I also could imagine to add a property ZeroLevelMode to TAreaSeries (zlmConstant, zlmData) which the zero level (end of the filled areas underneath the curves) would allow the zero level to pin to the first y values of the chartsource. This would simplify creating the banded series of the examples above because the ChartStyles would not be needed any more.

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #10 on: July 16, 2018, 08:28:03 pm »
Unfortunately, the examples don't seem to work with the Mac.
My problem is that I don't have a Mac, so I cannot test...

Transparency was added by Alexander Klenin some years ago, and I remember that it was some kind of hack. I can try to extract the basic code to write a bug report; maybe you'd have to help me with creating such a sample demo.

But anyway: Did you try the BGRABitmapDrawer? It is easy to apply - see my first post.

[EDIT] I see in your signature that you are using Laz 1.8.4. Could you try Laz trunk? There is an enormous count of cocoa-related bug-reports which have been resolved recently.
« Last Edit: July 16, 2018, 08:48:07 pm by wp »

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #11 on: July 18, 2018, 09:29:30 pm »
TAreaSeries of Lazarus trunk has a new (boolean) property "Banded" which suppresses painting of the bottom stacked layer and facilitates drawing of banded curves like those shown above.

Use a chartsource with two y values, and set the first y value to the lower limit of the band and the second y value to the upper limit of the band. Alternatively, the second y values can also specify the band width if the property "Stacked" is set the true in addition to "Banded". In this simple case, no ChartStyles are needed to define the color of the band.

Sub-bands can be created by adding more y values.

See also: http://wiki.lazarus.freepascal.org/TAChart_documentation#Area_series

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #12 on: July 24, 2018, 11:06:54 am »
TLineSeries, TBSplineSeries, TCubicSplineSeries and TFitSeries now can display error bars. The size of the error bars is defined by properties XErrorBarData and YErrorBarData of the chart source, and the appearance of the error bars is defined by the properties XErrorBars and YErrorBars of the series.

See http://wiki.lazarus.freepascal.org/TAChart_documentation#Error_bars for more details. There is also a demo in folder "components/tachart/demo/errorbars".

wp

  • Hero Member
  • *****
  • Posts: 11857
Re: Transparent series
« Reply #13 on: August 20, 2018, 11:32:55 pm »
In r58746 TFitSeries was enhanced by more versatile fitting routines. It is now possible to freeze parameters at a predefined value (e.g. force a fitted line to pass through the origin). Error bars defined by the chart source are used as weights for the fitting process. A new property "Statistics" presents a statistical analysis for the fit results (standard errors, t and p values for the fitted parameters, several goodness-of-fit parameters such as R2, adjusted R2, p value).

The demo "fit" in the "demo" folder of the TAChart installation was extended to show the new features.

 

TinyPortal © 2005-2018