1. 程式人生 > 其它 >DevExpress之圖表 (ChartControl)

DevExpress之圖表 (ChartControl)

1、自定義圖表資料(顯示各國人口的折線圖)

private void Bind()
{
    List<CountryInfo> countries = new List<CountryInfo> {
        new CountryInfo("中國",14.47f),
        new CountryInfo("印度",14.03f),
        new CountryInfo("美國",3.34f),
        new CountryInfo("印度尼西亞",2.78f),
        new CountryInfo("巴基斯坦",2.28f),
        new CountryInfo("奈及利亞",2.15f),
        new CountryInfo("巴西",2.14f),
        new CountryInfo("孟加拉國",1.67f),
        new CountryInfo("俄羅斯",1.45f),
        new CountryInfo("墨西哥",1.31f),
        new CountryInfo("日本",1.26f),
    };
            

    // 獲取已經設定的Series
    //Series s1 = chartControl1.Series[0];
    //s1.Name = "國家人口";
    //s1.DataSource = countries;
    //s1.ArgumentDataMember = "Name";
    //s1.ValueDataMembers[0] = "Population";
    //// 不顯示label(影象上是否顯示的內容)
    //s1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.False;
    ////定義X軸的資料的型別。質量,數字,時間
    //s1.ArgumentScaleType = ScaleType.Auto;
    ////定義Y軸的資料的型別。質量,數字,時間
    //s1.ValueScaleType = ScaleType.Numerical;
            

    //線條的型別,虛線,實線
    // 自定義的Series
    Series s2 = new Series("圖例說明",ViewType.Spline);
    s2.DataSource = countries;
    s2.ArgumentDataMember = "Name";
    s2.ValueDataMembers[0] = "Population";
    LineSeriesView view = s2.View as LineSeriesView;
    //定義線條上點的標識形狀 ,只有ViewType是Line時有效
    //view.LineMarkerOptions.Kind = MarkerKind.Circle;
    // 線條的型別,虛線,實線
    //view.LineStyle.DashStyle = DashStyle.DashDotDot;

    chartControl1.Series.Add(s2);

    // 水平調整
    chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Right;
    // 垂直調整
    chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.Top;
    // 圖表樣式,是否帶有選擇框
    chartControl1.Legend.MarkerMode = LegendMarkerMode.MarkerAndCheckBox;
    // 是否顯示圖例
    chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.True;

    XYDiagram xyD =  chartControl1.Diagram as XYDiagram;
    // X座標軸文字傾斜45度
    xyD.AxisX.Label.Angle = 45;
    // Y座標軸文字傾斜45度
    //xyD.AxisY.Label.Angle = 45;
    s2.Label.LineVisibility = DevExpress.Utils.DefaultBoolean.True;
    // 是否顯示圖例說明文字
    chartControl1.Legend.TextVisible = true;
}

2、查詢資料庫,顯示各月工資的柱狀圖

private void barLargeButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    string sql = "select t.salary_date , t.Real_salary from salary_sheet t where t.salary_type = '工資' order by t.salary_date";

    DataTable dtSalary = SqliteHelper.ExecuteQuery(sql);
    Series s1 = new Series("工資表", ViewType.Bar)
    {
        DataSource = dtSalary,                            
    };

    // 獲取柱子的物件
    SideBySideBarSeriesView v1 = s1.View as SideBySideBarSeriesView;
    // 修改柱子的寬度
    v1.BarWidth = 10;
    // 修改柱子的顏色
    v1.Color = Color.FromArgb(227, 108, 9);
    s1.ArgumentScaleType = ScaleType.DateTime;
    s1.ValueScaleType = ScaleType.Numerical;
    s1.ArgumentDataMember = "salary_date";     
    s1.ValueDataMembers[0] = "Real_salary";
    chartControl1.Series.Clear();
    chartControl1.Series.Add(s1);
}