memo

プログラミング備忘録

【C#】Chartグラフ-時計-(2)

役に立つのか? Chartで 謎グラフを作ってしまう……( ^ω^)

前回(1)
shinopikapi.hateblo.jp

手順
1. 目盛り線を消去
2. 円の枠を作成
3. 針を作成(2点取る線グラフ)
4. timerで360°針を動かす(秒針)

前回の続き(3、4)から

3. 針を作成(2点取る線グラフ)
f:id:shinopikapi:20190203003236p:plain

下記init_chart()はコンストラクタで呼ぶ。

        private string Name1 = "Bar1";
        private string AreaName1 = "chartArea1";
        private string Watch1 = "seconds";
        private const int RCIRCLE = 100;
        private const int WRANGE1 = 90;

        private void init_chart(Chart chart, int range)
        {
            // ~設定等~(前回(1)記載)

            // 円描画
            circle(chart, Name1, range);

            // 時計の針設定
            init_watch1(chart, Watch1, WRANGE1); // chart、Seriesの名前、半径
        }

init_watch1()で針の設定を行います。
簡単に書くと、Chartの折れ線グラフのタイプで2点取って直線にします。
円の中心(0,0)が固定で、適当に半径の値を設定したもう1点がぐるぐる回ります。

        private void init_watch1(Chart chart, string name, int radius)
        {
            chart.Series.Add(name);// Series追加
            chart.Series[name].ChartType = SeriesChartType.Line;// chart種類:折れ線
            chart.Series[name].Color = Color.DarkGray;
            chart.Series[name].BorderWidth = 4; // 線太さ[f:id:shinopikapi:20190202233948p:plain]

            // 初期値
            chart.Series[name].Points.Clear();
            double x = 0;
            double y = 0;

            // 原点
            chart.Series[name].Points.Add(new DataPoint(x, y));
            // 2点目 i=角度
            /*
            int i = 0;
            x = Math.Sin((i * Math.PI) / 180) * (double)radius;
            y = Math.Cos((i * Math.PI) / 180) * (double)radius;
            chart.Series[name].Points.Add(new DataPoint(x, y));
            */
        }

4. timerで360°針を動かす(秒針)
timerの設定もコンストラクタで行う。

            timer1.Enabled = true;
            timer1.Interval = 1000;
            timer1.Start();

Timerについては、以前備忘録としてまとめていました。
shinopikapi.hateblo.jp

Timerを1000ms間隔で動くように設定しているので、この間隔で処理します。
デバッグとして、今何秒かわかるようにラベルで表示してみました。
update_watch1()で針の描画を行います。

        private void Timer1_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now; // 現在時間
            label1.Text = now.ToString("yyyy/MM/dd HH:mm:ss");

            update_watch1(chart1, Watch1, WRANGE1, now.Second);
        }

f:id:shinopikapi:20190203005055p:plain

        private void update_watch1(Chart chart, string name, int radius, int sec)
        {
            // 秒->角度
            int i = 0;
            if (sec >= 0 && sec < 60)
            {
                i = sec * 6;
            }

            // 前回の針のポイント消去
            chart.Series[name].Points.Clear();
            double x = 0;
            double y = 0;

            // 原点
            chart.Series[name].Points.Add(new DataPoint(x, y));
            // 2点目 i=角度

            x = Math.Sin((i * Math.PI) / 180) * (double)radius;
            y = Math.Cos((i * Math.PI) / 180) * (double)radius;
            chart.Series[name].Points.Add(new DataPoint(x, y));
        }

今更ですが、Chartを画面上で設定したくないのでしていません。
全部コードで設定しています。

これを応用すれば、分針、短針も作れると思います。


参考
ChartAxis Class (Microsoft.ReportingServices.OnDemandReportRendering) | Microsoft Docs