zedgraph 折线图与柱形图,X轴文字竖向显示

 //柱状图创建函数

public void creatZZT(ZedGraphControl zgc, Dictionary<int, int> dic)
        {
         
            const double offset = 10;
            // 为每个点加标注
            GraphPane myPane = zgc.GraphPane;
            // Set the GraphPane title
            myPane.Title.Text = "像元统计图";
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times New Roman";

            myPane.XAxis.Title.Text = "像元值";
            myPane.YAxis.Title.Text = "像元数量";
            PointPairList list1 = new PointPairList();

            string[] labels = new string[dic.Count];
            for (int i = 0; i < dic.Count; i++)
            {
                list1.Add(i, dic[i]); //添加一组数据
                
            }
            labels[0] = "0-1";
            labels[1] = "1-5";
            labels[2] = ">5";
            double max = 0;
            foreach (double v in dic.Values) {
                if (v > max) {
                    max = v;

                }
            }
           myPane.AddBar("像元数", list1, Color.Black).Bar.Fill = new Fill(Color.Red, Color.White, Color.Red, 0f);
            for (int i = 0; i < labels.Length; i++)
            {
                // Get the pointpair
                //PointPair pt = list1[i].Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(dic[i].ToString(), i+1, dic[i] + max*1.0/20, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Red;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }

            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);


            myPane.XAxis.Scale.TextLabels = labels; //X轴文本取值

            myPane.XAxis.Type = AxisType.Text;   //X轴类型


            zgc.AxisChange();

            Refresh();
        }

//折线图创建函数

 public void creatpan(ZedGraphControl zgc, Dictionary<string, double> dic1, Dictionary<string, double> dic2, Dictionary<string, double> dic3)
        {
            const double offset = 10;
            // 为每个点加标注
            GraphPane myPane = zgc.GraphPane;
            // Set the GraphPane title
            myPane.Title.Text = "分区像元统计图";
            myPane.Title.FontSpec.IsItalic = true;
            myPane.Title.FontSpec.Size = 24f;
            myPane.Title.FontSpec.Family = "Times New Roman";

            myPane.XAxis.Title.Text = "区县名称";
            myPane.YAxis.Title.Text = "像元值";
            PointPairList list1 = new PointPairList();
            PointPairList list2 = new PointPairList();
            PointPairList list3 = new PointPairList();
            string[] labels = new string[dic1.Count];
            for (int i = 0; i < dic1.Count; i++)
            {
                list1.Add(i+1, Convert.ToDouble(dic1[returnDicKey(dic1,i)])); //添加一组数据
                list2.Add(i+1, Convert.ToDouble(dic2[returnDicKey(dic2, i)])); //添加一组数据
                list3.Add(i+1, Convert.ToDouble(dic3[returnDicKey(dic3, i)])); //添加一组数据

            }
            int count = 0;
            foreach(string ss in dic1.Keys)
            {
                labels[count] = ss;
                count++;


            }
           LineItem curve= myPane.AddCurve("最小值", list1, Color.Black, SymbolType.Star);
           LineItem curve2= myPane.AddCurve("区域均值", list2, Color.Blue, SymbolType.Star);
           LineItem curve3= myPane.AddCurve("最大值", list3, Color.Red, SymbolType.Star);
            
            myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
            //让X坐标名字竖着显示
            List<string> list = new List<string>();
            for (int i = 0; i < labels.Length; i++)
            {
                string value = "";
                for (int j = 0; j < labels[i].Length; j++)
                {
                    value += labels[i][j] + "\r\n";
                }
                list.Add(value);
            }
            string[] listvalue = new string[list.Count];
            for (int k = 0; k < list.Count; k++)
            {
                listvalue[k] = list[k];
            }

            myPane.XAxis.Scale.TextLabels = listvalue; //X轴文本取值
           double offset2 = 1;
            // Loop to add text labels to the points
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve.Points[i];
                // Create a text label from the Y data value

                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".")+3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Black;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve2.Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".") + 3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Blue;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            for (int i = 0; i < listvalue.Length; i++)
            {
                // Get the pointpair
                PointPair pt = curve3.Points[i];
                // Create a text label from the Y data value
                TextObj text = new TextObj(pt.Y.ToString().Substring(0, pt.Y.ToString().IndexOf(".") + 3), pt.X, pt.Y + offset2, CoordType.AxisXYScale, AlignH.Left, AlignV.Center);

                text.ZOrder = ZOrder.A_InFront;
                // Hide the border and the fill
                text.FontSpec.Border.IsVisible = false;
                text.FontSpec.Fill.IsVisible = false;
                text.FontSpec.Angle = 1;  //字体倾斜度
                text.FontSpec.Size = 13;
                text.FontSpec.FontColor = Color.Red;
                //myPane.GraphObjList.Add(text);
                myPane.GraphObjList.Add(text);
            }
            myPane.XAxis.MajorTic.IsBetweenLabels = true;
            myPane.XAxis.Type = AxisType.Text;   //X轴类型


            zgc.AxisChange();

            Refresh();
        }

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C#中使用ZedGraph实时绘制数据曲线,并且将X设置为时间,你可以使用XDate类型来表示时间,并在图表上设置适当的X格式。下面是一个示例代码,展示了如何实现实时数据曲线,其中X表示时间。 ```csharp using System; using System.Drawing; using System.Windows.Forms; using ZedGraph; namespace RealTimeGraph { public partial class Form1 : Form { private ZedGraphControl zedGraphControl1; private GraphPane graphPane; private RollingPointPairList dataPoints; private LineItem curve; private DateTime startTime; private Timer timer; public Form1() { InitializeComponent(); // 创建ZedGraph控件 zedGraphControl1 = new ZedGraphControl(); zedGraphControl1.Dock = DockStyle.Fill; this.Controls.Add(zedGraphControl1); // 创建图表对象 graphPane = zedGraphControl1.GraphPane; // 设置图表的标题 graphPane.Title.Text = "实时数据曲线示例"; // 设置X和Y的标签 graphPane.XAxis.Title.Text = "时间"; graphPane.YAxis.Title.Text = "数据值"; // 创建数据点列表 dataPoints = new RollingPointPairList(1000); // 创建曲线对象 curve = graphPane.AddCurve("曲线名称", dataPoints, Color.Blue, SymbolType.None); // 创建定时器 timer = new Timer(); timer.Interval = 1000; // 每隔1秒更新一次数据 timer.Tick += Timer_Tick; startTime = DateTime.Now; } private void Form1_Load(object sender, EventArgs e) { // 启动定时器 timer.Start(); } private void Timer_Tick(object sender, EventArgs e) { // 更新数据 double newData = GetNewData(); // 添加新数据点 double xAxisValue = (DateTime.Now - startTime).TotalSeconds; dataPoints.Add(xAxisValue, newData); // 刷新图表 zedGraphControl1.AxisChange(); zedGraphControl1.Invalidate(); } private double GetNewData() { // 这里可以根据你的需求获取新的数据值,这里仅作示例,使用随机数生成 Random random = new Random(); return random.Next(1, 10); } } } ``` 在这个示例中,我们使用了`DateTime.Now`来表示X的时间。在定时器的Tick事件中,我们计算从开始时间到当前时间的时间差,并将其作为X的值。然后,将新的数据点添加到数据点列表中。 请注意,为了使X能够正确显示时间,在图表对象的X属性中设置了适当的格式化。你可以根据自己的需求进行格式化。 希望这个示例对你有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值