我无法在折线图中绘制数据,我正在获取数据,但无法在折线图中绘制数据。
java文件
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import java.util.ArrayList;
public class DataExample extends AppCompatActivity {
private static final String TAG = "DataExample";
private LineChart mChart;
//ArrayList<Entry> x;
//ArrayList<String> x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_example);
//x = new ArrayList<Entry>();
//x = new ArrayList<String>();
mChart = (LineChart) findViewById(R.id.lineChart);
mChart.setDrawGridBackground(false);
//mChart.setDescription("");
mChart.setTouchEnabled(true);
mChart.setDragEnabled(true);
mChart.setScaleEnabled(true);
mChart.setPinchZoom(true);
//x = new ArrayList<Entry>();
//y = new ArrayList<String>();
//mChart = (LineChart) findViewById(R.id.lineChart);
//getHeroes();
//mChart.setOnChartGestureListener(LineChartActivity.this);
//mChart.setOnChartValueSelectedListener(LineChartActivity.this);
//mChart.setDragEnabled(true);
//mChart.setScaleEnabled(false);
mChart.setTouchEnabled(true);
mChart.setPinchZoom(true);
mChart.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
ArrayList<Entry> yValues = new ArrayList<>();
yValues.add(new Entry(0, 60f));
yValues.add(new Entry(1, 50f));
yValues.add(new Entry(2, 70f));
yValues.add(new Entry(3, 30f));
yValues.add(new Entry(4, 50f));
yValues.add(new Entry(5, 60f));
LineDataSet set1 = new LineDataSet(yValues, "Data Set 1");
set1.setFillAlpha(110);
set1.setColor(Color.RED);
set1.setLineWidth(3f);
set1.setValueTextSize(15f);
ArrayList<ILineDataSet> dataSet = new ArrayList<>();
dataSet.add(set1);
LineData data = new LineData(dataSet);
mChart.setData(data);
//String[] values = new String[] {"Jan", "Feb", "Mar", "Apr", "May", "Jun"};
getdata();
//XAxis xAxis = mChart.getXAxis();
//xAxis.setValueFormatter(new MyAxisValueFormatter(values));
//String[] values = new String[] {"1", "2", "3", "4", "5", "6" };
//XAxis xAxis = mChart.getXAxis();
//xAxis.setValueFormatter(new MyAxisValueFormatter(values));
}
private void getdata() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
ApiInterface api = retrofit.create(ApiInterface.class);
Call<List<Data>> call = api.getdata();
call.enqueue(new Callback<List<Data>>(){
@Override
public void onResponse(Call<List<Data>> call, Response<List<Data>> response) {
List<Data> namelist = response.body();
//int[] heroes = new int[heroList.size()];
String[] names = new String[namelist.size()];
for (int i = 0; i < namelist.size(); i++) {
//int value = jsonObject.getInt("value");
//String value[i] = heroList.get(i).getName();
//String values = heroList.get(i).getName();
//String date = jsonObject.getString("time");
//String created = heroList.get(i).getCreatedby();
//x.add(new Entry(value, i));
//x.add(value);
//XAxis xAxis = mChart.getXAxis();
//xAxis.setValueFormatter(new MyAxisValueFormatter(values));
names[i] = namelist.get(i).getName();
//String value = heroList.get(i).getCreatedby();
//x.add(new Entry(heroes[i],i));
//y.add(value);
}
XAxis xAxis = mChart.getXAxis();
xAxis.setValueFormatter(new MyAxisValueFormatter(names));
//listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));
}
@Override
public void onFailure(Call<List<Data>> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public class MyAxisValueFormatter implements IAxisValueFormatter {
private String[] mValues;
public MyAxisValueFormatter(String[] values){
this.mValues = values;
}
@Override
public String getFormattedValue(float value, AxisBase axis) {
return mValues[(int)value];
}
}
}
JSON数据:
[{
"name": "Captain America",
"realname": "Steve Rogers",
"team": "Avengers",
"firstappearance": "1941",
"createdby": "Joe Simon",
"publisher": "Marvel Comics",
"imageurl": "https:\/\/www.simplifiedcoding.net\/demos\/marvel\/captainamerica.jpg",
"bio": "\r\n\t\tSteven Rogers was born in the Lower East Side of Manhattan, New York City, in 1925 to poor Irish immigrants, Sarah and Joseph Rogers.[54] Joseph died when Steve was a child, and Sarah died of pneumonia while Steve was a teen. By early 1940, before America's entry into World War II, Rogers is a tall, scrawny fine arts student specializing in illustration and a comic book writer and artist.\r\n\t\t"
}, {
"name": "Iron Man",
"realname": "Tony Stark",
"team": "Avengers",
"firstappearance": "1963",
"createdby": "Stan Lee",
"publisher": "Marvel Comics",
"imageurl": "https:\/\/www.simplifiedcoding.net\/demos\/marvel\/ironman.jpg",
"bio": "\r\n\t\tAnthony Edward Stark, the son of wealthy industrialist and head of Stark Industries, Howard Stark, and Maria Stark. A boy genius, he enters MIT at the age of 15 to study electrical engineering and later receives master's degrees in electrical engineering and physics. After his parents are killed in a car accident, he inherits his father's company.\r\n\t\t"
}, {
"name": "Wolvarine",
"realname": "James Howlett",
"team": "X-Men",
"firstappearance": "1974",
"createdby": "Len Wein",
"publisher": "Marvel Comics",
"imageurl": "https:\/\/www.simplifiedcoding.net\/demos\/marvel\/logan.jpg",
"bio": "\r\n\t\tWolverine was born James Howlett in northern Alberta, Canada, during the late 1880s, purportedly to rich farm owners John and Elizabeth Howlett,[27] though he is actually the illegitimate son of the Howletts' groundskeeper, Thomas Logan.[28] After Thomas is thrown off the Howletts' property for an attempted rape perpetrated by his other son, named simply Dog, he returns to the Howlett manor and kills John Howlett. In retaliation, young James kills Thomas with bone claws that emerge from the back of his hands, as his mutation manifests.[29] He flees with his childhood companion, Rose, and grows into manhood on a mining colony in the Yukon, adopting the name \"Logan\".[30] When Logan accidentally kills Rose with his claws, he flees the colony and lives in the wilderness among wolves,[31] until he is captured and placed in a circus.[32] Saul Creed, brother of Victor Creed, frees Logan, but after he betrays Logan and Clara Creed to Nathaniel Essex, Logan drowns Creed in Essex's potion.[33] Logan returns to civilization, residing with the Blackfoot people. Following the death of his Blackfoot lover, Silver Fox, at the hands of Victor Creed, now known as Sabretooth,[34] he is ushered into the Canadian military during World War I. Logan spends time in Madripoor before settling in Japan, where he marries Itsu and has a son, Daken. Logan is unaware of his son for many years.\r\n\t\t"
}, {
"name": "Spiderman",
"realname": "Peter Parker",
"team": "Avengers",
"firstappearance": "1962",
"createdby": "Stan Lee",
"publisher": "Marvel Comics",
"imageurl": "https:\/\/www.simplifiedcoding.net\/demos\/marvel\/spiderman.jpg",
"bio": "\r\n\t\tIn Forest Hills, Queens, New York,[43] Midtown High School student Peter Parker is a science-whiz orphan living with his Uncle Ben and Aunt May. As depicted in Amazing Fantasy #15 (Aug. 1962), he is bitten by a radioactive spider (erroneously classified as an insect in the panel) at a science exhibit and \"acquires the agility and proportionate strength of an arachnid\".[44] Along with super strength, Parker gains the ability to adhere to walls and ceilings. Through his native knack for science, he develops a gadget that lets him fire adhesive webbing of his own design through small, wrist-mounted barrels. Initially seeking to capitalize on his new abilities, Parker dons a costume and, as \"Spider-Man\", becomes a novelty television star. However, \"He blithely ignores the chance to stop a fleeing thief, [and] his indifference ironically catches up with him when the same criminal later robs and kills his Uncle Ben.\" Spider-Man tracks and subdues the killer and learns, in the story's next-to-last caption, \"With great power there must also come-great responsibility!\"[45]\r\n\t\t"
}, {
"name": "Thor",
"realname": "Thor Odinson",
"team": "Avengers",
"firstappearance": "1962",
"createdby": "Stan Lee",
"publisher": "Marvel Comics",
"imageurl": "https:\/\/www.simplifiedcoding.net\/demos\/marvel\/thor.jpg",
"bio": "\r\n\t\tThor's father Odin decides his son needed to be taught humility and consequently places Thor (without memories of godhood) into the body and memories of an existing, partially disabled human medical student, Donald Blake.[52] After becoming a doctor and on vacation in Norway, Blake witnesses the arrival of an alien scouting party. Blake flees from the aliens into a cave. After discovering Thor's hammer Mjolnir (disguised as a walking stick) and striking it against a rock, he transforms into the thunder god.[53] Later, in Thor #159, Blake is revealed to have always been Thor, Odin's enchantment having caused him to forget his history as The Thunder God and believe himself mortal.[54]\r\n\t\t"
}]
答案 0 :(得分:0)
也许您不是要在Y-axis
中添加数据,所以请尝试以下
private void getdata() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.BASE_URL)
.addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
.build();
ApiInterface api = retrofit.create(ApiInterface.class);
Call<List<Data>> call = api.getdata();
call.enqueue(new Callback<List<Data>>(){
@Override
public void onResponse(Call<List<Data>> call, Response<List<Data>> response) {
List<Data> namelist = response.body();
//int[] heroes = new int[heroList.size()];
String[] names = new String[namelist.size()];
String[] value = new String[namelist.size()];
for (int i = 0; i < namelist.size(); i++) {
//int value = jsonObject.getInt("value");
//String value[i] = heroList.get(i).getName();
//String values = heroList.get(i).getName();
//String date = jsonObject.getString("time");
//String created = heroList.get(i).getCreatedby();
//x.add(new Entry(value, i));
//x.add(value);
//XAxis xAxis = mChart.getXAxis();
//xAxis.setValueFormatter(new MyAxisValueFormatter(values));
names[i] = namelist.get(i).getName();
value[i] = namelist.get(i).getCreatedby();
//x.add(new Entry(heroes[i],i));
//y.add(value);
}
XAxis xAxis = mChart.getXAxis();
xAxis.setValueFormatter(new MyAxisValueFormatter(names));
YAxis yAxis = mChart.getYAxis();
yAxis.setValueFormatter(new MyAxisValueFormatter(value));
mChart.notifyDataSetChanged(); //also add this line
//listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));
}