我编写了一个代码来生成Highcharts library的图表数据,并且需要知道是否有任何可能的改进。我已经应用了泛型,但认为它可以进一步调整。有人可以推荐什么吗?
我基本上写了一个 GenerateBoxPlotChartSeries 方法,它会生成 BoxPlotChartSeries ,顾名思义。
有两种方法具有相同的名称,接受不同的参数。拥有两种不同的方法是不是一个好主意,或者更好的方法是使用通用方法并将泛型应用于参数,然后根据参数进行检查并执行 if else 方法内的条件?我唯一担心的是方法的长度会不断增加。另请注意,我将创建更多图表,如折线图和样条图。类和方法也需要考虑到这一点。因此,该方法还需要相应地生成样条图数据和折线图数据
请注意以下事项 1. NpvGraphs将包含CommIns,CaptiveIns,SelfIns和EVAGraphs将包含CaptiveView和ParentView。
我很快会将其他图表与 boxplotchart 区分开来。我该如何处理?
public class Series<T>
{
public List<T> data { get; set; }
}
public class BloxPlotSeriesData
{
[JsonMinify]
public double low { get; set; }
[JsonMinify]
public double q1 { get; set; }
[JsonMinify]
public double median { get; set; }
[JsonMinify]
public double q3 { get; set; }
[JsonMinify]
public double high { get; set; }
[JsonMinify]
public string Color { get; set; }
}
public class NPVGraphData
{
[JsonMinify]
public List<double> CommIns { get; set; }
[JsonMinify]
public List<double> CaptiveIns { get; set; }
[JsonMinify]
public List<double> SelfIns { get; set; }
}
public class EVAGraphData
{
[JsonMinify]
public List<double> CaptiveView { get; set; }
[JsonMinify]
public List<double> ParentView { get; set; }
}
调用方法
NpvResult = new Models.NpvResults()
{
SeriesData = GenerateBoxPlotChartSeries(trigger2Output.NpvResults.GraphData)
}
EvaResult = new Models.EvaResults()
{
SeriesData =GenerateBoxPlotChartSeries(trigger2Output.EvaResults.GraphData)
}
类似地,我将再对该方法进行两次调用,相应地生成样条图和折线图。上面的方法调用仅适用于BoxPlotChart。
StressResult = new Models.StressTestAnalysis()
{
SeriesData=GenerateSplineChartSeries(trigger2Output.StressTestAnalysis.GraphData)
}
EndingSurplus = new EndingSurplus()
{
SeriesData=GenerateLineChartSeries(trigger2Output.EndingSurplus.GraphData)
}
方法
private Series<BloxPlotSeriesData> GenerateBoxPlotChartSeries(EVAGraphData evaResultsGraphData)
{
//CaptiveView
var captiveViewSeriesData = new BloxPlotSeriesData
{
low = evaResultsGraphData.CaptiveView[0],
q1 = evaResultsGraphData.CaptiveView[1],
median = evaResultsGraphData.CaptiveView[2],
q3 = evaResultsGraphData.CaptiveView[3],
high = evaResultsGraphData.CaptiveView[4],
Color = "#C111A0"
};
//ParentView
var parentViewSeriesData = new BloxPlotSeriesData
{
low = evaResultsGraphData.ParentView[0],
q1 = evaResultsGraphData.ParentView[1],
median = evaResultsGraphData.ParentView[2],
q3 = evaResultsGraphData.ParentView[3],
high = evaResultsGraphData.ParentView[4],
Color = "#FFB81C"
};
return new Series<BloxPlotSeriesData>
{
data = new List<BloxPlotSeriesData> { captiveViewSeriesData, parentViewSeriesData }
};
}
private Series<BloxPlotSeriesData> GenerateBoxPlotChartSeries(NPVGraphData npvResultsGraphData)
{
//CaptiveIns
var captiveInsSeriesData = new BloxPlotSeriesData
{
low = npvResultsGraphData.CaptiveIns[0],
q1 = npvResultsGraphData.CaptiveIns[1],
median = npvResultsGraphData.CaptiveIns[2],
q3 = npvResultsGraphData.CaptiveIns[3],
high = npvResultsGraphData.CaptiveIns[4],
Color = "#C111A0"
};
//CommIns
var commInsSeriesData = new BloxPlotSeriesData
{
low = npvResultsGraphData.CommIns[0],
q1 = npvResultsGraphData.CommIns[1],
median = npvResultsGraphData.CommIns[2],
q3 = npvResultsGraphData.CommIns[3],
high = npvResultsGraphData.CommIns[4],
Color = "#FFB81C"
};
//SelfIns
var selfInsSeriesData = new BloxPlotSeriesData
{
low = npvResultsGraphData.SelfIns[0],
q1 = npvResultsGraphData.SelfIns[1],
median = npvResultsGraphData.SelfIns[2],
q3 = npvResultsGraphData.SelfIns[3],
high = npvResultsGraphData.SelfIns[4],
Color = "#5D63D3"
};
return new Series<BloxPlotSeriesData>
{
data = new List<BloxPlotSeriesData>{ captiveInsSeriesData, commInsSeriesData, selfInsSeriesData }
};
}
答案 0 :(得分:0)
我不知道你是否真的需要泛型,因为你的工作似乎相当专业而且不是非常“通用”:你需要从共享某种结构的类中提取绘图数据。 也许一个简单的类层次结构会怎样? 我所做的只是:
如果您有任何问题或建议,请与我们联系。
public class Series<T>
{
public List<T> Data { get; set; }
}
public class BloxPlotSeriesData
{
[JsonMinify]
public double Low { get; set; }
[JsonMinify]
public double Q1 { get; set; }
[JsonMinify]
public double Median { get; set; }
[JsonMinify]
public double Q3 { get; set; }
[JsonMinify]
public double High { get; set; }
[JsonMinify]
public string Color { get; set; }
}
/// <summary>
/// The base class, holding common drawing data.
/// </summary>
public abstract class GraphData
{
private const string CaptivePlotColor = "#C111A0";
private const string ParentPlotColor = "#FFB81C";
[JsonMinify]
public List<double> CaptiveView { get; set; }
[JsonMinify]
public List<double> ParentView { get; set; }
/// <summary>
/// Override this method in subclasses if you needto provide additional data.
/// </summary>
/// <returns>More data to be plotted.</returns>
public virtual BloxPlotSeriesData ReturnMoreData()
{ return null; }
/// <summary>
/// Returns plot data.
/// </summary>
/// <returns>Plot data.</returns>
public Series<BloxPlotSeriesData> GenerateBoxPlotChartSeries()
{
BloxPlotSeriesData captiveViewSeriesData = GetCaptivePlotData();
BloxPlotSeriesData parentViewSeriesData = GetParentData();
var Result = new Series<BloxPlotSeriesData>
{
Data = new List<BloxPlotSeriesData> { captiveViewSeriesData, parentViewSeriesData }
};
return AddMoreData(Result);
}
/// <summary>
/// Adds more data if the first argument can provide it.
/// </summary>
/// <param name="Result"></param>
/// <returns></returns>
private Series<BloxPlotSeriesData> AddMoreData(Series<BloxPlotSeriesData> Result)
{
var MoreData = ReturnMoreData();
if (MoreData != null)
{
Result.Data.Add(MoreData);
}
return Result;
}
private BloxPlotSeriesData GetParentData()
{
return new BloxPlotSeriesData
{
Low = ParentView[0],
Q1 = ParentView[1],
Median = ParentView[2],
Q3 = ParentView[3],
High = ParentView[4],
Color = ParentPlotColor
};
}
private BloxPlotSeriesData GetCaptivePlotData()
{
return new BloxPlotSeriesData
{
Low = CaptiveView[0],
Q1 = CaptiveView[1],
Median = CaptiveView[2],
Q3 = CaptiveView[3],
High = CaptiveView[4],
Color = CaptivePlotColor
};
}
}
public class EVAGraphData : GraphData
{
}
public class NPVGraphData : GraphData
{
[JsonMinify]
public List<double> SelfIns { get; set; }
public override BloxPlotSeriesData ReturnMoreData()
{
return new BloxPlotSeriesData
{
Low = SelfIns[0],
Q1 = SelfIns[1],
Median = SelfIns[2],
Q3 = SelfIns[3],
High = SelfIns[4],
Color = "#5D63D3"
};
}
}