Forum > TAChart

Lines in different places on the chart.

(1/2) > >>

Seenkao:
Могу ли я с помощью TChart "нарисовать" линии в разных местах графика? Линии не должны соединяться. Линии могут быть построены произвольно.

Can I use TChart to "draw" lines in different places on the chart? The lines must not connect. Lines can be drawn arbitrarily.

wp:
Sorry I don't understand... Could you try to draw a sketch of what the chart should look like and post the image here?

Nicole:
Quite certain "yes".
Check the DEMO-folder of TAChart.

Have a glance at "userdrawn" although I thought, that there must be another one much better.

Seenkao:
nan-demo показывает как можно сделать что-то подобное.

nan-demo shows how you can do something similar.

wp:
One possibility to connect pairs of points with lines in TAChart is the use the TFieldSeries. It was originally intended to draw a field of vectors to illustrate, for example, the flow direction and velocity of a streaming liquid or gas. But since it consisits of two (x,y) pairs per datapoint, it can be used for your purpose.

The method to add data points (here: lines) to this series is AddVector which gets two (x,y) pairs as arguments. Note, however, that they are not the start and end points of the line, but the center point and the direction vector. You can calculate the center of the connectionline between two points P1 and P2 as (P1 + P2) * 0.5, and the direction vector is P2 - P1. When you add unit TAGeometry to the "uses" clause of your form, these expressions really can be calculated exactly like this because TAGeometry implements overloaded simple arithmetic operators for the TDoublePoint type (otherwise you would have to calculate the X and Y coordinates separately, eg. center.X := (P1.X + P2.X) * 0.5; center.Y := (P1.Y + P2.Y) * 0.5).

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{ Adds a line to the series which connects the points P1 and P2. The line  is drawn in the specified color. }procedure TForm1.AddLine(ASeries: TFieldSeries; P1, P2: TDoublePoint; AColor: TColor);var  center: TDoublePoint;  direction: TDoublePoint;begin  center := (P1 + P2) * 0.5;  direction := P2 - P1;  ASeries.AddVector(center.X, center.Y, direction.X, direction.Y, '', AColor);end;
You should also turn off the Arrow of the FieldSeries which otherwise would show up at the endpoint.

Study the attached demo project.