chart control setting bar and point percent in c#

0 thích 0 không thích
1 lượt xem
đã hỏi 17 Tháng 1 bởi nguyenthao (9,040 điểm)
using DevExpress.XtraCharts;
using DevExpress.XtraCharts.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BarChartDemo
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        class DataDemo
        {
            public string name { get; set; }
            public int value { get; set; }
            public string percent { get; set; }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            var listData = new List<DataDemo>();
            listData.Add(new DataDemo() { name = "Apple", value = 1500, percent = "50%"});
            listData.Add(new DataDemo() { name = "Banana", value = 450, percent = "50%" });
            listData.Add(new DataDemo() { name = "Orange", value = 250, percent = "50%" });
            listData.Add(new DataDemo() { name = "Lemon", value = 3500, percent = "50%" });
            listData.Add(new DataDemo() { name = "Strawberry", value = 800, percent = "50%" });
            listData.Add(new DataDemo() { name = "Longan", value = 15, percent = "50%" });
            listData.Add(new DataDemo() { name = "Mango", value = 0, percent = "50%" });
            listData.Add(new DataDemo() { name = "Avocado", value = 560, percent = "50%" });
            listData.Add(new DataDemo() { name = "Cherry", value = 150, percent = "50%" });

            chartControl1.DataSource = listData;

            Series series1 = new Series("fruit", ViewType.Bar);
            series1.ArgumentDataMember = "name";
            series1.ValueDataMembers.AddRange(new string[] { "value" }) ;
            
            


            series1.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;
           // series1.Label.TextPattern = "{V1:#,###.##} - {V2}";
            series1.View.Color = ColorTranslator.FromHtml("#21A1F1");
            series1.Label.TextAlignment = StringAlignment.Far;


            Series series2 = new Series("percent", ViewType.Point);
            series2.ArgumentDataMember = "name";
            series2.ValueDataMembers.AddRange(new string[] { "value" });
          // ((LineSeriesView)series2.View).MarkerVisibility = DevExpress.Utils.DefaultBoolean.False;
            series2.LabelsVisibility = DevExpress.Utils.DefaultBoolean.True;


            //((LineSeriesView)series2.View).LineStyle.DashStyle = DashStyle.Dot;


            DevExpress.XtraCharts.PointSeriesView pointSeriesView = (DevExpress.XtraCharts.PointSeriesView)series2.View;

            // Hide markers by setting the MarkerVisibility property
            pointSeriesView.PointMarkerOptions.Size = 1;


            chartControl1.Series.AddRange(new Series[] { series1, series2 });
            var label = chartControl1.Series[0].Label as SideBySideBarSeriesLabel;
            if (label != null)
            {
                label.Position = BarSeriesLabelPosition.Center;
            }

            var label2 = chartControl1.Series[1].Label as PointSeriesLabel;
            if (label2 != null)
            {
                label2.Position = PointLabelPosition.Outside;
                label2.TextAlignment = StringAlignment.Center;
                label2.TextColor = Color.Green;
                label2.LineColor = Color.Red;
                label2.BackColor = Color.Yellow;
            }

            ((XYDiagram)chartControl1.Diagram).Rotated = true;
            ((XYDiagram)chartControl1.Diagram).AxisX.Tickmarks.MinorVisible = false;
            ((XYDiagram)chartControl1.Diagram).AxisX.Reverse = true;
            // Disable minor tickmarks on the x-axis:
            XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            diagram.AxisX.Tickmarks.MinorVisible = false;

            diagram.AxisX.GridLines.MinorVisible = false;
            diagram.AxisY.GridLines.MinorVisible = false;
            diagram.AxisX.GridLines.Visible = false;
            diagram.AxisY.GridLines.Visible = false;
            diagram.AxisY.Label.Visible = false;
            diagram.AxisY.Visibility = DevExpress.Utils.DefaultBoolean.False;
            diagram.DefaultPane.BorderVisible = false;

            diagram.AxisX.Label.TextPattern = "{V:#,###.##}";         
            chartControl1.Titles.Add(new ChartTitle { Text = "Sales by Products - LAPTRINHVB.NET", DXFont = new Font("Tahoma", 14, FontStyle.Bold), TextColor=Color.Orange });

            chartControl1.CustomDrawSeriesPoint += ChartControl1_CustomDrawSeriesPoint;
            chartControl1.Legend.Visibility = DevExpress.Utils.DefaultBoolean.False;
            chartControl1.Legend.MarkerMode = LegendMarkerMode.None;
            chartControl1.Legend.AlignmentHorizontal = LegendAlignmentHorizontal.Center;
            chartControl1.Legend.AlignmentVertical = LegendAlignmentVertical.BottomOutside;
            chartControl1.Legend.Direction = LegendDirection.LeftToRight;
            chartControl1.BorderOptions.Visibility = DevExpress.Utils.DefaultBoolean.False;
            chartControl1.BorderOptions.Color = Color.Red;





        }

        private void ChartControl1_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
           if(e.Series.ToString() == "percent")
            {
                var data = sender as ChartControl;
                var datasource = data.DataSource as List<DataDemo>;
                var name = e.SeriesPoint.Argument;
                var percent = datasource.Where(x => x.name == name).FirstOrDefault().percent;
                var value = e.SeriesPoint.Values[0];
                e.LabelText = $"{percent}";
                
                
            }

           
           
        }
    }
}

Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...