溫馨提示×

c# plotly怎樣設(shè)置圖表樣式

c#
小樊
82
2024-09-17 01:10:48
欄目: 編程語言

在C#中,使用Plotly庫設(shè)置圖表樣式需要先安裝Plotly.NET包

Install-Package Plotly.NET -Version 2.0.0-preview.6

接下來,你可以使用以下代碼示例創(chuàng)建一個簡單的折線圖并自定義其樣式:

using System;
using Plotly.NET;
using Plotly.NET.TraceObjects;

namespace PlotlyExample
{
    class Program
    {
        static void Main(string[] args)
        {
            // 創(chuàng)建數(shù)據(jù)
            double[] x = new double[] { 1, 2, 3, 4, 5 };
            double[] y = new double[] { 2, 4, 6, 8, 10 };

            // 創(chuàng)建折線圖
            var line = Chart.Line<double, double, double>(x, y);

            // 設(shè)置圖表標(biāo)題和軸標(biāo)簽
            var chart = Chart.Plot<double, double, double>(line)
                .WithTitle("My Line Chart")
                .WithXTitle("X Axis")
                .WithYTitle("Y Axis");

            // 設(shè)置線條樣式
            chart.WithLine(new Line()
            {
                Color = "red",
                Width = 2,
                Dash = Dash.Dot
            });

            // 設(shè)置圖表布局
            chart.WithLayout(new Layout()
            {
                PaperBgColor = "white",
                PlotBgColor = "white",
                Font = new Plotly.NET.LayoutObjects.Font()
                {
                    Family = "Arial",
                    Size = 12,
                    Color = "black"
                }
            });

            // 顯示圖表
            chart.Show();
        }
    }
}

這個示例展示了如何創(chuàng)建一個簡單的折線圖,并設(shè)置圖表標(biāo)題、軸標(biāo)簽、線條樣式和圖表布局。你可以根據(jù)需要修改這些設(shè)置以滿足你的需求。更多關(guān)于Plotly.NET的信息和示例,請參閱官方文檔:https://plotly.net/

0