AnyChart试图显示一个活动的2个折线图



大家好,我正试图在一个活动上显示两个折线图。当我将代码更改为只显示一个图表时,我正在使用AnyChart。但当我添加第二个时,只有那个显示,第一个不见了。

这是的活动

公共类DetailedFieldActivity扩展了AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detailed_field);
Intent intentData = getIntent();
String FieldName = intentData.getStringExtra("field");
setTitle(FieldName);

prepareGraphTop();
prepareGraphBot();
}
public void prepareGraphTop() {
Intent graphsIntent = getIntent();
String response = graphsIntent.getStringExtra("response");

AnyChartView top = findViewById(R.id.top_soil);
top.setProgressBar(findViewById(R.id.progress_bar));
Cartesian topChartData = AnyChart.line();
topChartData.animation(true);
topChartData.padding(5d, 10d, 3d, 10d);
topChartData.xAxis(0).scale();
topChartData.xScroller(true).container();
topChartData.crosshair().enabled(true);
topChartData.crosshair()
.yLabel(true)
.yStroke((Stroke) null, null, null, (String) null, (String) null);
topChartData.tooltip().positionMode(TooltipPositionMode.POINT);
topChartData.title("Top Soil (0 - 400mm)");
topChartData.xAxis(0).labels().padding(3d, 3d, 3d, 3d);
final List<DataEntry> seriesDataTop = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(response);
JSONObject graphs = jsonResponse.getJSONObject("Grafieke");
JSONArray dates = graphs.names();
for (int i = 3; i < graphs.length(); i++) {
String dateName = dates.getString(i);
JSONObject dateData = graphs.getJSONObject(dateName);
Number topSoil = dateData.getInt("TB");
Number bb = dateData.getInt("BB");
Number stressBo = dateData.getInt("stress");
Number verwelpBo = dateData.getInt("verwelp");
seriesDataTop.add(new CustomDataTop(dateName, topSoil, bb, stressBo));
}
} catch (JSONException e) {
e.printStackTrace();
}
Set setTop = Set.instantiate();
setTop.data(seriesDataTop);
Mapping seriesTopMapping1 = setTop.mapAs("{ x: 'x', value: 'value' }");
Mapping seriesTopMapping2 = setTop.mapAs("{ x: 'x', value: 'value2' }");
Mapping seriesTopMapping3 = setTop.mapAs("{ x: 'x', value: 'value3' }");
Line seriesTop1 = topChartData.line(seriesTopMapping1);
seriesTop1.name("TB");
seriesTop1.hovered().markers().enabled(true);
seriesTop1.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesTop1.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line seriesTop2 = topChartData.line(seriesTopMapping2);
seriesTop2.name("TO");
seriesTop2.hovered().markers().enabled(true);
seriesTop2.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesTop2.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line seriesTop3 = topChartData.line(seriesTopMapping3);
seriesTop3.name("Stress Bo");
seriesTop3.hovered().markers().enabled(true);
seriesTop3.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesTop3.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
topChartData.legend().enabled(true);
topChartData.legend().fontSize(13d);
topChartData.legend().padding(0d, 0d, 10d, 0d);
top.setChart(topChartData);
}
public void prepareGraphBot() {
Intent graphsIntent = getIntent();
String response = graphsIntent.getStringExtra("response");
AnyChartView bottom = findViewById(R.id.bottom_soil);
bottom.setProgressBar(findViewById(R.id.progress_bar));
Cartesian botChartData = AnyChart.line();
botChartData.animation(true);
botChartData.padding(5d, 10d, 3d, 10d);
botChartData.xAxis(0).scale();
botChartData.xScroller(true).container();
botChartData.crosshair().enabled(true);
botChartData.crosshair()
.yLabel(true)
.yStroke((Stroke) null, null, null, (String) null, (String) null);
botChartData.tooltip().positionMode(TooltipPositionMode.POINT);
botChartData.title("Bottom Soil (400 - 800mm)");
botChartData.xAxis(0).labels().padding(3d, 3d, 3d, 3d);
final List<DataEntry> seriesDataBot = new ArrayList<>();
try {
JSONObject jsonResponse = new JSONObject(response);
JSONObject graphs = jsonResponse.getJSONObject("Grafieke");
JSONArray dates = graphs.names();
for (int i = 3; i < graphs.length(); i++) {
String dateName = dates.getString(i);
JSONObject dateData = graphs.getJSONObject(dateName);
Number botSoil = dateData.getInt("TO");
Number bo = dateData.getInt("BO");
Number stressOnder = dateData.getInt("stressonder");
Number verwelpOnder = dateData.getInt("verwelponder");
Number pvrOnder = dateData.getInt("pvronder");
seriesDataBot.add(new CustomDataBot(dateName, botSoil, bo, stressOnder));
}
} catch (JSONException e) {
e.printStackTrace();
}
Set setBot = Set.instantiate();
setBot.data(seriesDataBot);
Mapping seriesBotMapping1 = setBot.mapAs("{ x: 'r', value: 'value12' }");
Mapping seriesBotMapping2 = setBot.mapAs("{ x: 'r', value: 'value22' }");
Mapping seriesBotMapping3 = setBot.mapAs("{ x: 'r', value: 'value32' }");
Line seriesBot1 = botChartData.line(seriesBotMapping1);
seriesBot1.name("TO");
seriesBot1.hovered().markers().enabled(true);
seriesBot1.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesBot1.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line seriesBot2 = botChartData.line(seriesBotMapping2);
seriesBot2.name("BO");
seriesBot2.hovered().markers().enabled(true);
seriesBot2.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesBot2.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
Line seriesBot3 = botChartData.line(seriesBotMapping3);
seriesBot3.name("Stress Onder");
seriesBot3.hovered().markers().enabled(true);
seriesBot3.hovered().markers()
.type(MarkerType.CIRCLE)
.size(4d);
seriesBot3.tooltip()
.position("right")
.anchor(Anchor.LEFT_CENTER)
.offsetX(5d)
.offsetY(5d);
botChartData.legend().enabled(true);
botChartData.legend().fontSize(13d);
botChartData.legend().padding(0d, 0d, 10d, 0d);
bottom.setChart(botChartData);
}
private class CustomDataTop extends ValueDataEntry {
CustomDataTop(String x, Number value, Number value2, Number value3) {
super(x, value);
setValue("value2", value2);
setValue("value3", value3);
}
}
private class CustomDataBot extends ValueDataEntry {
CustomDataBot(String r, Number value12, Number value22, Number value32) {
super(r, value12);
setValue("value22", value22);
setValue("value32", value32);
}
}

}

这是我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".DetailedFieldActivity">
<com.anychart.AnyChartView
android:id="@+id/top_soil"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="49dp"
android:layout_marginEnd="0dp">
</com.anychart.AnyChartView>
<com.anychart.AnyChartView
android:id="@+id/bottom_soil"
android:layout_width="match_parent"
android:layout_height="201dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginBottom="50dp">
</com.anychart.AnyChartView>
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="278dp"
android:layout_height="189dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="53dp"
android:layout_marginTop="159dp"
android:layout_marginEnd="53dp"
android:layout_marginBottom="163dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/top_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:background="@android:color/transparent"
android:text="Top Bototm Soil" />
<Button
android:id="@+id/depths"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:layout_above="@+id/top_soil"
android:layout_centerHorizontal="true"
android:layout_marginBottom="3dp"
android:background="@android:color/transparent"
android:text="Depths" />
<Button
android:id="@+id/soil_temps"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="0dp"
android:background="@android:color/transparent"
android:text="Soil Temps" />
<Button
android:id="@+id/photo"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/transparent"
android:text="Photos" />
<Button
android:id="@+id/moisture"
android:layout_width="121dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:text="Moisture" />
<Button
android:id="@+id/irrigation"
android:layout_width="123dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:background="@android:color/transparent"
android:text="Irrigation" />
</RelativeLayout>

你知道怎么了吗?

在获得chartwiew 的引用后更改此行

//prepareGraphTop method
AnyChartView top = findViewById(R.id.top_soil);
APIlib.getInstance().setActiveAnyChartView(top);
//prepareGraphBot method 
AnyChartView bottom = findViewById(R.id.bottom_soil);
APIlib.getInstance().setActiveAnyChartView(bottom);

您可以查看文档单击此处!

MPAndroid图表

强大的🚀安卓图表视图/图表视图库,支持条形图、饼图、雷达图、气泡图和蜡烛图以及缩放、拖动、动画和MultipleCharts。

在一个xml文件中实现多个图表更容易

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/pie_chart_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/card_layout_padding"
android:elevation="@dimen/elevation"
app:cardCornerRadius="@dimen/card_radius">
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/pie_chart"
android:layout_width="match_parent"
android:layout_height="@dimen/card_size_chart_pie" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="@+id/line_chart_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pie_chart_card"
android:layout_marginBottom="@dimen/card_layout_padding"
android:elevation="@dimen/elevation"
app:cardCornerRadius="@dimen/card_radius">
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/line_chart"
android:layout_width="match_parent"
android:layout_height="@dimen/card_size_chart" />
</android.support.v7.widget.CardView>
</RelativeLayout>

最新更新