gpt4 book ai didi

java - 如何使用 ValueProvider 将通用垂直轴添加到图表中

转载 作者:行者123 更新时间:2023-12-01 14:43:28 27 4
gpt4 key购买 nike

这就是我目前向图表添加一条线的方法。这是我想要显示的任意函数的抽象类:

public abstract class ArbitraryFunction implements
ValueProvider<ArbitraryFunctionData, Double> {

private String field;

public abstract Double f(Double x);

/**
* Constructor
*/
public ArbitraryFunction(String field) {
this.field = field;
}

@Override
public Double getValue(ArbitraryFunctionData object) {
return object.get(field);
}

@Override
public void setValue(ArbitraryFunctionData object, Double value) {
object.put(field, value);
}

@Override
public String getPath() {
return field;
}
}

这就是图表的创建方式:

ArbitraryFunction f1 = new ArbitraryFunction("f1") {
@Override
public Double f(Double x) {
return Math.sin(x);
}
};

functionMap.put(f1.getPath(), f1);

// collects the data of the functions and adds them to the store
for (Double x = 0.0; x <= 2 * Math.PI; x = x + 0.1) {
ArbitraryFunctionData d = new ArbitraryFunctionData();
d.setName("" + x);
for (Map.Entry<String, ArbitraryFunction> entry : functionMap.entrySet()) {
ArbitraryFunction tmp = entry.getValue();
d.put(tmp.getPath(), tmp.f(x));
}
store.add(d);
}

chart.setStore(store);

verticalAxis.setPosition(Position.LEFT);
verticalAxis.addField(f1);
verticalAxis.setSteps(2);
verticalAxis.setMinorTickSteps(5);
chart.addAxis(verticalAxis);

到目前为止,这符合预期。该图显示了我的线条,它应该这样做,并且垂直轴也是正确的。但我在绘制水平轴时遇到问题,因为我不知道需要提供什么horizo​​ntalAxis.addField(???)。我尝试了一些方法,但没有任何效果。

有谁知道我需要如何设置水平轴?

最佳答案

您希望横轴值是多少?是另一个 NumericAxis - 每个数据点是否都有 x应该利用什么值(value)?每个d在你的循环中有一个字符串 name和一些值(value) - 也许你想要一个 CategoryAxis<ArbitraryFunctionData, String>这只是吸引那些 name值(value)观?

<小时/>

看起来我之前误解了 - 您的 Function 对象只是在设置中使用,而不是在更改绘制数据的方式中使用

我仍然不确定你在追求什么,但听起来你最想绘制一些线条。每个数据点(ArbitraryFunctionData?)似乎都有所使用的每个函数的 Y 值和标题,但没有 X 值,因此无法将每个点绘制为带有两个数字轴的 (X,Y),就像 ( name, Y) 使用 CategoryAxis 和 NumericAxis。这或多或少会像这个示例一样:http://www.sencha.com/examples/#ExamplePlace:linechart - 底部有字符串,侧面有数字。

这是一个,主要基于您已有的想法/结构:

public class FunctionPlotter implements EntryPoint {
public static class ArbitraryFunctionData {
private double xValue;
private Map<String, Double> yValues = new HashMap<String, Double>();
public double get(String key) {
return yValues.get(key);
}
public void put(String key, double yValue) {
yValues.put(key, yValue);
}
public double getXValue() {
return xValue;
}
public void setxValue(double xValue) {
this.xValue = xValue;
}
}
public interface AFDProperties extends PropertyAccess<ArbitraryFunctionData> {
//xvalue is unique, key off of that
@Path("xValue")
ModelKeyProvider<ArbitraryFunctionData> key();

//automatic ValueProvider generation for the get/setXValue methods
ValueProvider<ArbitraryFunctionData, Double> xValue();
}

/**
* This is really doing two different jobs at once - wasn't quite was I was trying to suggest in
* that other question. See the second version of this for clarification...
*/
public static abstract class ArbitraryFunction implements ValueProvider<ArbitraryFunctionData, Double> {
private final String field;

public ArbitraryFunction(String field) {
this.field = field;
}

public abstract Double f(Double x);

@Override
public Double getValue(ArbitraryFunctionData object) {
return object.get(field);
}

@Override
public void setValue(ArbitraryFunctionData object, Double value) {
object.put(field, value);
}

@Override
public String getPath() {
return field;
}
}

@Override
public void onModuleLoad() {
Viewport vp = new Viewport();

Set<ArbitraryFunction> functions = new HashSet<ArbitraryFunction>();
ArbitraryFunction f1 = new ArbitraryFunction("f1") {
@Override
public Double f(Double x) {
return Math.sin(x);
}
};
functions.add(f1);

AFDProperties props = GWT.create(AFDProperties.class);
ListStore<ArbitraryFunctionData> store = new ListStore<ArbitraryFunctionData>(props.key());
// collects the data of the functions and adds them to the store
for (Double x = 0.0; x <= 2 * Math.PI; x = x + 0.1) {
// Create one data object, and set the X value, since that is the same for all Y values
ArbitraryFunctionData d = new ArbitraryFunctionData();
d.setxValue(x);

// For each function, set the corresponding Y value
for (ArbitraryFunction func : functions) {
d.put(func.getPath(), func.f(x));
}
store.add(d);
}

Chart<ArbitraryFunctionData> chart = new Chart<ArbitraryFunctionData>();
chart.setStore(store);

//Y-axis
NumericAxis<ArbitraryFunctionData> verticalAxis = new NumericAxis<ArbitraryFunctionData>();
verticalAxis.setPosition(Position.LEFT);
verticalAxis.addField(f1);//needs to know this field to properly set the range of values
//f2, f3, etc
verticalAxis.setSteps(2);
verticalAxis.setMinorTickSteps(5);
chart.addAxis(verticalAxis);

// X-Axis, this time reading from the xValue, not the series of ValueProviders
NumericAxis<ArbitraryFunctionData> horizAxis = new NumericAxis<ArbitraryFunctionData>();
horizAxis.setPosition(Position.BOTTOM);
horizAxis.addField(props.xValue());//same value for all
horizAxis.setSteps(2);
horizAxis.setMinorTickSteps(5);
chart.addAxis(horizAxis);

for (ArbitraryFunction func : functions) {
LineSeries<ArbitraryFunctionData> line = new LineSeries<ArbitraryFunctionData>();
// configure x axis
line.setXAxisPosition(Position.BOTTOM);//where is it
line.setXField(props.xValue());//what value do i use
// configure y axis
line.setYAxisPosition(Position.LEFT);//where is it
line.setYField(func);//what value do i use

//probably want to customized per func
line.setStroke(RGB.GRAY);
line.setStrokeWidth(2);

chart.addSeries(line);
}

vp.setWidget(chart);
RootPanel.get().add(vp);
}
}

这里有两个,这次使用更简单的数据,实际上使函数成为它自己的 ValueProvider,并保持数据简单 - 只是一个 double !请注意,ValueProvider函数,我们从不自己调用 getValue,我们让轴/系列为我们做这件事!此处添加了第二个函数以证明它确实有效。

public class FunctionPlotter implements EntryPoint {

/**
* Where did everything go? We're just making a ValueProvider now that can handle
* each number as a value, and working out the details from there
*
* For fun, added per-function coloring too
*/
public abstract static class Function implements ValueProvider<Double, Double> {
private final String name;
private final Color color;
public Function(String name, Color color) {
this.name = name;
this.color = color;
}
@Override
public abstract Double getValue(Double object);

@Override
public String getPath() {
return name;
}
@Override
public void setValue(Double object, Double value) {
//no-op
}
public Color getColor() {
return color;
}
}

@Override
public void onModuleLoad() {
Viewport vp = new Viewport();

Set<Function> functions = new HashSet<Function>();
Function f1 = new Function("f1", RGB.RED) {
@Override
public Double getValue(Double x) {
return Math.sin(x);
}
};
functions.add(f1);
Function f2 = new Function("f2", RGB.BLACK) {
@Override
public Double getValue(Double x) {
return Math.cos(x);
}
};
functions.add(f2);

//Turns out Stores can hold any objects - should probably factor out this key provider for reuse...
ListStore<Double> store = new ListStore<Double>(new ModelKeyProvider<Double>() {
@Override
public String getKey(Double item) {
return item.toString();
}
});
// collects the data of the functions and adds them to the store
for (Double x = 0.0; x <= 2 * Math.PI; x = x + 0.1) {
store.add(x);
}

Chart<Double> chart = new Chart<Double>();
chart.setStore(store);

//Y-axis
NumericAxis<Double> verticalAxis = new NumericAxis<Double>();
verticalAxis.setPosition(Position.LEFT);
for (Function func : functions) {
verticalAxis.addField(func);//needs to know this field to properly set the range of values
}
verticalAxis.setSteps(2);
verticalAxis.setMinorTickSteps(5);
chart.addAxis(verticalAxis);

// X-Axis, this time reading from the xValue, not the series of ValueProviders
NumericAxis<Double> horizAxis = new NumericAxis<Double>();
horizAxis.setPosition(Position.BOTTOM);
horizAxis.addField(new IdentityValueProvider<Double>());//magic value provider that returns the same string
horizAxis.setSteps(2);
horizAxis.setMinorTickSteps(5);
chart.addAxis(horizAxis);

for (Function func : functions) {
LineSeries<Double> line = new LineSeries<Double>();
// configure x axis
line.setXAxisPosition(Position.BOTTOM);//where is it
line.setXField(new IdentityValueProvider<Double>());//what value do i use
// configure y axis
line.setYAxisPosition(Position.LEFT);//where is it
line.setYField(func);//what value do i use

//probably want to customized per func
line.setStroke(func.getColor());
line.setStrokeWidth(2);

chart.addSeries(line);
}

vp.setWidget(chart);
RootPanel.get().add(vp);
}
}

关于java - 如何使用 ValueProvider 将通用垂直轴添加到图表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15705496/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com