Help us understand the problem. What is going on with this article?

C++ Builder XE4 > TeeChart > クリックした点に近い系列データの明示 > 点と値の表示 > v0.4: クリック時に縦方向に線を明示するようにする

More than 1 year has passed since last update.
動作環境
C++ Builder XE4
TeeChart Lite v2013.08.130414 32bit VCL

前回

v0.4

クリック時に縦方向に線を明示するようにする。

TShapeを使う。

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.ExtCtrls.hpp>
#include <VCLTee.Chart.hpp>
#include <VCLTee.TeEngine.hpp>
#include <VCLTee.TeeProcs.hpp>
#include <VCLTee.Series.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TChart *Chart1;
    TFastLineSeries *Series1;
    TPointSeries *Series2;
    TEdit *E_xvalue;
    TEdit *E_yvalue;
    TButton *Button1;
    TShape *Shape1;
    void __fastcall FormShow(TObject *Sender);
    void __fastcall Chart1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y);

private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <DateUtils.hpp>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    Shape1->Brush->Color = clRed;
//  Shape1->Brush->Style = bsClear;
//  Shape1->Brush->Style = bsHorizontal;
//  Shape1->Brush->Style = bsFDiagonal;
    Shape1->Visible = false;
    Shape1->Pen->Style = psClear;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    // テスト用の系列データ追加

    Chart1->Series[0]->XValues->DateTime = true;
    Chart1->BottomAxis->DateTimeFormat = L"nn:ss";

    TDateTime dt;

    dt = Now();

    double yval;
    for (int idx=0; idx < 10; idx++) {
        yval = (1+ idx) % 2;
        Series1->AddXY(dt, yval, "", clRed);
        dt = IncSecond(dt, 1);
    }
}

#define POINT_NOT_FOUND (-1)

//---------------------------------------------------------------------------
void __fastcall TForm1::Chart1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
          int X, int Y)
{
    // グラフ上端から下端まで走査してインデックスを見つける
    int top = Chart1->ChartRect.Top;
    int btm = Chart1->ChartRect.Bottom;
    int index = POINT_NOT_FOUND;  // 系列のインデックスが入る
    for(int ypos = top; ypos <= btm; ypos++) {
        index = Series1->Clicked(X, ypos);
        if (index != POINT_NOT_FOUND) {
            break;
        }
    }

    // A. クリックが外れていたとき
    if (index == POINT_NOT_FOUND) {
        return;
    }

    // B. クリックが線上の時に表示

    // 枠の追加
    double xval = Series1->XValue[index];
    double yval = Series1->YValue[index];

    //   枠
    Shape1->Visible = true;
    Shape1->Width = 4;
    Shape1->Left = Series1->CalcXPosValue(xval);
    Shape1->Left -= Shape1->Width / 2; // 真ん中に来るように
    Shape1->Top = Chart1->ChartRect.top;
    Shape1->Height = Chart1->ChartRect.Height();

    // 数値表示
    E_xvalue->Text = String().sprintf(L"%.3f", xval);
    E_yvalue->Text = String().sprintf(L"%.3f", yval);
}
//---------------------------------------------------------------------------

動作例

2018-11-27_17h47_14.png

今回、Series2は使っていない。

参考

情報感謝です。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away