我目前有一个TChart,我想引入一个可拖动的水平线,它可以改变线下方点的颜色。我选择使用ColorLine用于此目的,但该行不会出现在TChart中。我使用正确的TChart工具还是我错过了什么?
以下是我当前代码的精简版。
public class testClass
{
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public testClass()
{
line = new Colorline();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
}
答案 0 :(得分:1)
以下代码在这里对我有用。如果问题仍然存在,请发送a Short, Self Contained, Correct (Compilable), Example project以重现此问题。您可以在www.steema.net/upload发布文件。
using Steema.TeeChart;
using Steema.TeeChart.Styles;
using Steema.TeeChart.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
InitializeChart();
}
private void InitializeChart()
{
testClass();
//existing code which assigns colors
tChart1.Series.Add(new Steema.TeeChart.Styles.Bar()).FillSampleValues();
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
tChart1.Series[0].ColorRange(tChart1.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
tChart1.Tools.Add(line);
line.Active = true;
line.Axis = tChart1.Axes.Left;
line.Value = lineYVal;
}
private ColorLine line;
private double lineYVal = 5;
private TChart savedChart;
public void testClass()
{
line = new ColorLine();
line.AllowDrag = true;
line.Pen.Color = Color.Red;
line.EndDragLine += lineDragHandler;
}
public void foo(TChart chart) //chart is prepopulated with datapoints from 0->10
{
savedChart = chart;
//existing code which assigns colors
chart.Series[0].ColorRange(chart.Series[0].YValues, double.MinValue, lineYVal, Color.Red);
chart.Series[0].ColorRange(chart.Series[0].YValues, lineYVal, double.MaxValue, Color.Blue);
//my attempt to add a line
chart.Tools.Add(line);
line.Active = true;
line.Axis = chart.Axes.Left;
line.Value = lineYVal;
}
private void lineDragHandler(object sender)
{
lineYVal = line.Value;
if (savedChart != null)
{
savedChart.Tools.Clear(); //remove existing line from chart
foo(savedChart); //redo colors and re-add line
}
else
{
foo(tChart1);
}
}
}
}
答案 1 :(得分:0)
事实证明,当正确添加该行时,我用来显示图表的代码没有正确更新图表,并且在将图表传递到我的高亮显示功能之前显示了该图表的版本。