gpt4 book ai didi

java - 切换图例可见性时 TChart 崩溃?

转载 作者:行者123 更新时间:2023-12-01 14:57:20 25 4
gpt4 key购买 nike

当我在不同的 TChart 系列之间切换时,如果在运行之间切换图例可见性,则会出现异常。例如

  1. 显示条形图。图例可见。
  2. 显示饼图。传说看不见。
  3. 显示条形图。图例可见。
  4. 崩溃!

这是在重新绘制控件时导致未捕获异常的行:

chart.getLegend().setVisible(true);

在每次运行之间我都会执行以下操作:

chart.setAutoRepaint(false);
chart.removeAllSeries();
// Build chart...
chart.setAutoRepaint(true);
chart.refreshControl();

TChart 专家们,我怎样才能避免这种崩溃?

最佳答案

这对我来说不会崩溃。我可以多次从 Bar 切换到 Pie,没有任何问题。

package com.steema.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;

import com.steema.teechart.TChart;
import com.steema.teechart.styles.Bar;
import com.steema.teechart.styles.Pie;
import com.steema.teechart.themes.ThemesList;

public class AndroidTestActivity extends Activity implements OnItemSelectedListener{

private TChart tChart1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout group = (LinearLayout) findViewById(R.id.linearLayoutTchart);

tChart1 = new TChart(this);
group.addView(tChart1);
ThemesList.applyTheme(tChart1.getChart(), 1);

Spinner spinner = (Spinner) findViewById(R.id.series_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.series_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tChart1.setAutoRepaint(false);
tChart1.removeAllSeries();
switch (arg2) {
case 0:
Bar bar1 = new Bar(tChart1.getChart());
bar1.fillSampleValues();
tChart1.getLegend().setVisible(true);
break;
case 1:
Pie pie1 = new Pie(tChart1.getChart());
pie1.fillSampleValues();
tChart1.getLegend().setVisible(false);
break;
}
tChart1.setAutoRepaint(true);
tChart1.refreshControl();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}
}

这是我的 main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<Spinner android:id="@+id/series_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout android:id="@+id/linearLayoutTchart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

这是我的 strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">TeeChartJava for Android testing application!</string>
<string name="app_name">AndroidTest</string>
<string-array name="series_array">
<item>Bar</item>
<item>Pie</item>
</string-array>
</resources>
<小时/>

更新1:如果我在 case 1 上方添加 tChart1.getLegend().setSeries(tChart1.getSeries(0));:

    case 1:
Pie pie1 = new Pie(tChart1.getChart());
pie1.fillSampleValues();
tChart1.getLegend().setSeries(tChart1.getSeries(0));
tChart1.getLegend().setVisible(false);
break;

然后,当我选择返回 Bar 系列时,我收到一条错误消息。这是因为我们将图例设置为使用饼图系列(第一次选择饼图),当我们选择条形图时,我们删除了该饼图系列,但图例仍然引用删除的饼图系列。请检查您是否为图例设置了有效的系列。即:

    case 0:
Bar bar1 = new Bar(tChart1.getChart());
bar1.fillSampleValues();
tChart1.getLegend().setSeries(tChart1.getSeries(0));
tChart1.getLegend().setVisible(true);
break;
case 1:
Pie pie1 = new Pie(tChart1.getChart());
pie1.fillSampleValues();
tChart1.getLegend().setSeries(tChart1.getSeries(0));
tChart1.getLegend().setVisible(false);
break;

下一版本将在清除系列列表后在removeAllSeries()中设置getLegend().setSeries(null)

关于java - 切换图例可见性时 TChart 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14187775/

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