LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > TeeChart > クリックした点に近い系列データの明示 > 点と値の表示 > v0.3: 線からはずれていても縦方向の走査により線を見つける

Last updated at Posted at 2018-11-18
動作環境
C++ Builder XE4
TeeChart Lite v2013.08.130414 32bit VCL

前回

UI > v0.1の問題点

v0.3

X座標はそのまま使い、Y座標はグラフの上端から下端までを走査する。

実装

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.Series.hpp>
#include <VCLTee.TeEngine.hpp>
#include <VCLTee.TeeProcs.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TChart *Chart1;
    TFastLineSeries *Series1;
    TEdit *E_xvalue;
    TEdit *E_yvalue;
    TLabel *Label1;
    TLabel *Label2;
    TPointSeries *Series2;
    void __fastcall FormCreate(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)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(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) {
        if (Series2->Count() > 0) {
            Series2->Delete(0);
            E_xvalue->Text = L"";
            E_yvalue->Text = L"";
        }
        return;
    }

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

    // 既存のものは消す
    if (Series2->Count() > 0) {
        Series2->Delete(0);
    }

    // 点の追加
    double xval = Series1->XValue[index];
    double yval = Series1->YValue[index];
    Series2->AddXY(xval, yval);

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

動作例

2018-11-18_13h45_49.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1