gpt4 book ai didi

android - 似乎无法弄清楚如何使用 HoloGraphLibrary 将点动态添加到折线图。

转载 作者:搜寻专家 更新时间:2023-11-01 08:48:39 25 4
gpt4 key购买 nike

我一直在研究如何实现折线图。我真的很喜欢来自 https://bitbucket.org/danielnadeau/holographlibrary/wiki/Home 的 HoloGraphLibrary .有人可以帮助我实现或让我朝着正确的方向前进以向折线图添加点。我希望使用 X 轴的数据库位置和 Y 轴的输入数据。

我不知道如何添加 LinePoint p = new LinePoint();。任何建议都会有所帮助。

全息图 XML

<com.echo.holographlibrary.LineGraph
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/graph"/>

全息图

    Line l = new Line();
LinePoint p = new LinePoint();
p.setX(0);
p.setY(5);
l.addPoint(p);
p = new LinePoint();
p.setX(8);
p.setY(8);
l.addPoint(p);
p = new LinePoint();
p.setX(10);
p.setY(4);
l.addPoint(p);
l.setColor(Color.parseColor("#FFBB33"));

LineGraph li = (LineGraph) findViewById(R.id.graph);
li.addLine(l);
li.setRangeY(0, 10);
li.setLineToFill(0);

最佳答案

使用 LineGraph.addPointToLine(lineIndex,newPointX,newPointY);

这是一个工作示例(请参阅按钮的 onClickListener)

MainActivity.java

public class MainActivity extends Activity {

LineGraph mLineGraph;
EditText mEditText;
float lastX; // Last x coordinate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources resources = getResources();

Line line = new Line();
line.setUsingDips(true);

// Initialize graph with some points
List<LinePoint> points = new ArrayList<LinePoint>(20);
points.add(new LinePoint(0,0));
points.add(new LinePoint(1,2));
points.add(new LinePoint(2,10));
points.add(new LinePoint(3,5));
lastX = 3;
for(LinePoint p : points) {
line.addPoint(p);
}

line.setColor(resources.getColor(R.color.orange));
mLineGraph = (LineGraph) findViewById(R.id.linegraph);
mLineGraph.setUsingDips(true);
mLineGraph.addLine(line);
mLineGraph.setRangeY(0, 10);
mLineGraph.setLineToFill(0);

mLineGraph.setOnPointClickedListener(new LineGraph.OnPointClickedListener() {
@Override
public void onClick(int lineIndex, int pointIndex) {
Toast.makeText(getApplicationContext(),
"Line " + lineIndex + " / Point " + pointIndex + " clicked",
Toast.LENGTH_SHORT)
.show();
}
});

mEditText = (EditText) findViewById(R.id.edittext);

Button mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Add point after the last X coord with Y coord from EditText
lastX++;
float newY = Float.parseFloat(mEditText.getText().toString());
mLineGraph.addPointToLine(0,lastX,newY);
}
});

}
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="Y coordinate"
android:id="@+id/edittext"/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Add another point"
android:id="@+id/button" />
</LinearLayout>

<com.echo.holographlibrary.LineGraph
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linegraph"
app:lineStrokeColor="@color/transparent_orange"
app:lineAxisColor="@color/transparent_blue"
app:lineStrokeWidth="1dip"
app:lineStrokeSpacing="10dip"
app:lineUseDip="true"/>

</LinearLayout>

colors.xml(取自 HoloGraph 示例应用)

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#FFFF0000</color>
<color name="blue">#FF0000FF</color>
<color name="transparent_blue">#800000FF</color>
<color name="green">#FF00FF00</color>
<color name="green_light">#FF99CC00</color>
<color name="orange">#FFFFBB33</color>
<color name="transparent_orange">#80FFBB33</color>
<color name="purple">#FFAA66CC</color>
</resources>

这是它的样子

HoloGraph sample

关于android - 似乎无法弄清楚如何使用 HoloGraphLibrary 将点动态添加到折线图。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25735663/

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