私はCandleStickChartからデータを取得し、これをLineChartに追加することに問題があります。CandleStickから値を取得し、LineChartに追加する方法 - > MPANDROIDCHART
この方法を試してみてください。私はCandleStickからデータを取得し、LineChartにデータを設定します。すべてが完璧に動作します。
しかし、私は文字にdynamicDataを追加しようとすると問題がある:私は、みんなが画像上に表示さあなたのような2つの文字を生成しています。私の方法は、CandleStickChartにデータを追加するだけです。
LineChartでデータを設定するメソッドは、ここにあります。CandleEntry lastEntry = set1.getEntryForIndex(i);
棒グラフの値がわかります。
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
ここで私はaddLineEntryですが、これは動作しません。理由はわかりません。
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineDataSet.getXMax() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
最後は、CandleStickChartからaddEntryまでの同様の方法です。
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
ここでCandleStickChartとLineChartからEntryを削除する方法ですが、これはうまくいきます。
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
皆さんは何が間違っていますか? ここisfullクラス:
public class MainGameFragment extends Fragment {
@BindView(R.id.spinner_money)
Spinner spinnerData;
@BindView(R.id.text_profit_ill)
TextView text_profit;
@BindView(R.id.button_cash)
Button btnCashCurrency;
@BindView(R.id.restart_game)
Button restartGame;
@BindView(R.id.butonCurrency)
Button buttonCurrency;
@BindView(R.id.chart)
CandleStickChart mChart;
@BindView(R.id.progress_bar)
ProgressBar progress;
@BindView(R.id.btn_buy)
Button btnBuy;
@BindView(R.id.btn_sell)
Button btnSell;
@BindView(R.id.drawer_settings)
ImageButton openDrawerSettings;
@BindView(R.id.chartLine)
LineChart lineChart;
public static ArrayList<String> HISTORYTRANSACTION = new ArrayList<>();
public static ArrayList<String> LEADERBOARDUSER = new ArrayList<>();
public static String userNameAndScore;
private Handler handler;
private Handler handlerLast;
private String buttonPosition;
int pos = 0;
LostDialogFragment lostFragment = LostDialogFragment.newInstance(1);
WinDialogFragment winFragment = WinDialogFragment.newInstance(1);
SettingsFragment settingsFragment = SettingsFragment.newInstance(1);
String DIALOG_WIN = "WinDialogFragment";
String DIALOG_LOST = "LostDialogFragment";
String DIALOG_SETTINGS = "settingsFragment";
private CandleData data;
private LineData lineData;
private LineDataSet lineDataSet;
private CandleDataSet set1;
private Drawer result;
public static StorageReference storageReference;
private Runnable r;
private Runnable rLast;
ArrayList<CandleEntry> yVals1 = new ArrayList<>();
ArrayList<Entry> entries = new ArrayList<>();
public MainGameFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getArguments();
if (bundle != null) {
buttonPosition = getArguments().getString("button_position", "value");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_game, container, false);
ButterKnife.bind(this, view);
setText();
configureSpinnerDataAndLogic();
configureChart();
configureColorProgresBar();
openDrawer();
configureDrawer();
welcomeMessage();
configureHandler(5000);
storageReference = FirebaseStorage.getInstance().getReference();
setData();
return view;
}
private void setData() {
int prog = 50;
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(i);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(i, lastOpenCloseMax));
}
XAxis xAxis = lineChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.TRANSPARENT);
xAxis.setDrawGridLines(false);
YAxis leftAxis = lineChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setAxisMaximum(1.6f);
leftAxis.setAxisMinimum(0.4f);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.TRANSPARENT);
YAxis rightAxis = lineChart.getAxisRight();
rightAxis.setEnabled(false);
Collections.sort(entries, new EntryXComparator());
lineDataSet = new LineDataSet(entries, "# of Calls");
lineData = new LineData(lineDataSet);
lineDataSet.setColor(Color.GRAY);
lineDataSet.setDrawCircles(false);
// dataset.setDrawFilled(true);
lineData.setValueTextColor(Color.TRANSPARENT);
lineChart.getLegend().setEnabled(false);
lineChart.getDescription().setEnabled(false);
lineChart.setBackgroundColor(Color.TRANSPARENT);
lineChart.setData(lineData);
lineChart.animateX(4000);
}
private void welcomeMessage() {
Toast.makeText(getContext(), "Welcome " + MainActivity.getUsername(getContext()).trim() + "!"
+ " Getting data from last hour..",
Toast.LENGTH_LONG).show();
}
private void configureDrawer() {
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(getActivity())
.withProfileImagesClickable(false)
.withHeaderBackground(R.drawable.logo_white)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withSliderBackgroundColor(Color.GRAY)
.withAccountHeader(headerResult)
.withActivity(getActivity())
.withDisplayBelowStatusBar(false)
.withDrawerGravity(Gravity.LEFT)
.withHeaderPadding(true)
.addDrawerItems(
new SectionDrawerItem().withName("Options"),
new PrimaryDrawerItem().withName("Trading History").withIcon(R.drawable.trading_history).withIdentifier(2),
new PrimaryDrawerItem().withName("Leader Board").withIcon(R.drawable.leade_board).withIdentifier(3),
new PrimaryDrawerItem().withName("Special offer").withIcon(R.drawable.special_icon).withIdentifier(4),
new PrimaryDrawerItem().withName("Video tutorials").withIcon(R.drawable.video_tutorials).withIdentifier(5),
new PrimaryDrawerItem().withName("FAQ").withIcon(R.drawable.faq_icon).withIdentifier(6),
new PrimaryDrawerItem().withName("CONTACT").withIcon(R.drawable.contact_icon).withIdentifier(7)
)
.buildForFragment();
result.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
SettingsFragment.POSITION = position;
result.closeDrawer();
if (settingsFragment != null) {
settingsFragment.show(ft, DIALOG_SETTINGS);
}
return true;
}
});
result.getDrawerLayout().setFitsSystemWindows(false);
result.getSlider().setFitsSystemWindows(false);
}
private CandleDataSet createSet() {
set1 = new CandleDataSet(null, "DataSet 1");
set1.setColor(Color.rgb(240, 99, 99));
set1.setHighLightColor(Color.rgb(190, 190, 190));
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setValueTextSize(10f);
return set1;
}
private LineDataSet createLineSet() {
lineDataSet = new LineDataSet(null, "DataSet 1");
lineDataSet.setColor(Color.rgb(240, 99, 99));
lineDataSet.setHighLightColor(Color.rgb(190, 190, 190));
lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
lineDataSet.setValueTextSize(10f);
return lineDataSet;
}
public void openDrawer() {
openDrawerSettings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.openDrawer();
}
});
}
private void addLineEntry() {
lineData = lineChart.getData();
if (lineDataSet == null) {
lineDataSet = createLineSet();
lineData.addDataSet(lineDataSet);
}
int prog = 1;
int xMax = (int) set1.getXMax();
for (int i = 0; i < prog; i++) {
CandleEntry lastEntry = set1.getEntryForIndex(xMax -1);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
entries.add(new Entry(lineData.getDataSetCount() +1, lastOpenCloseMax));
}
lineDataSet.notifyDataSetChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
lineChart.moveViewTo(lineChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
private void addEntry(boolean start) {
data = mChart.getData();
if (set1 == null) {
set1 = createSet();
data.addDataSet(set1);
}
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 1;
int xMax = (int) set1.getXMax();
CandleEntry lastEntry = set1.getEntryForIndex(yVals1.size() - 1);
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float lastOpenCloseMax = Math.max(lastEntry.getOpen(), lastEntry.getClose());
float currentOpenCloseMax = Math.max(open, close);
float currentOpenCloseMin = Math.min(open, close);
float high = open + 0.3f;
float low = close - 0.3f;
if (currentOpenCloseMax < lastOpenCloseMax) {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMax, currentOpenCloseMin));
} else {
yVals1.add(new CandleEntry(xMax + 1, high, low, currentOpenCloseMin, currentOpenCloseMax));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
mChart.moveViewTo(mChart.getXChartMax(), 2f, YAxis.AxisDependency.RIGHT);
}
}
private void removeLastEntry() {
CandleData data = mChart.getData();
if (data != null) {
ICandleDataSet set = data.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
mChart.notifyDataSetChanged();
mChart.invalidate();
}
}
LineData lineData = lineChart.getData();
if (lineData != null) {
ILineDataSet set = lineData.getDataSetByIndex(0);
if (set != null) {
set.removeFirst();
data.notifyDataChanged();
lineChart.notifyDataSetChanged();
lineChart.invalidate();
}
}
}
public String getUserInfoAndSave() {
userNameAndScore = MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim();
return userNameAndScore;
}
private void configureHandlerWithoutRemoveLastEntry(final int time) {
handlerLast = new Handler();
rLast = new Runnable() {
public void run() {
// removeLastEntry();
addEntry(true);
handler.postDelayed(this, time);
}
};
handlerLast.postDelayed(rLast, time);
}
private void configureHandler(final int time) {
handler = new Handler();
r = new Runnable() {
public void run() {
removeLastEntry();
addEntry(true);
addLineEntry();
handler.postDelayed(this, time);
}
};
handler.postDelayed(r, time);
}
public void stopLast() {
handlerLast.removeCallbacks(rLast);
}
public void stop() {
handler.removeCallbacks(r);
}
private void configureChart() {
mChart.getDescription().setEnabled(false);
mChart.getLegend().setTextColor(Color.WHITE);
mChart.setMaxVisibleValueCount(50);
XAxis xAxis = mChart.getXAxis();
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
xAxis.setTextColor(Color.WHITE);
xAxis.setDrawGridLines(false);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setLabelCount(12, false);
leftAxis.setDrawGridLines(false);
leftAxis.setDrawAxisLine(true);
leftAxis.setTextColor(Color.WHITE);
YAxis rightAxis = mChart.getAxisRight();
rightAxis.setEnabled(false);
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
int prog = 50;
float last = Float.NEGATIVE_INFINITY;
String date = String.valueOf(android.text.format.DateFormat.format("yyyy-MM-dd", new java.util.Date()));
for (int i = 0; i < prog; i++) {
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float max = Math.max(open, close);
if (last < max) {
float tmp = open;
open = close;
close = tmp;
}
last = max;
float high = open + 0.3f;
float low = close - 0.3f;
yVals1.add(new CandleEntry(i, high, low, open, close));
}
set1 = new CandleDataSet(yVals1, date);
data = new CandleData(set1);
mChart.setData(data);
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(Color.rgb(80, 80, 80));
set1.setIncreasingColor(Color.GREEN);
set1.setIncreasingPaintStyle(Paint.Style.FILL);
set1.setDecreasingColor(Color.RED);
set1.setDecreasingPaintStyle(Paint.Style.FILL);
set1.setNeutralColor(Color.BLUE);
set1.setBarSpace(0.2f);
set1.setValueTextColor(Color.TRANSPARENT);
mChart.notifyDataSetChanged();
mChart.animateX(4000);
}
private void setText() {
buttonCurrency.setText("Assets: \n" + buttonPosition);
}
private void configureColorProgresBar() {
progress.getIndeterminateDrawable().setColorFilter(
getResources().getColor(R.color.white),
android.graphics.PorterDuff.Mode.SRC_IN);
}
@OnClick(R.id.invest_text)
public void invest() {
float highmax = 1.0700f;
float highlow = 1.1700f;
float lowmax = 0.5700f;
float lowlow = 0.63000f;
float open = highlow + new Random().nextFloat() * (highmax - highlow);
float close = lowlow + new Random().nextFloat() * (lowmax - lowlow);
float high = open + 0.3f;
float low = close - 0.3f;
if (pos == 0) {
yVals1.add(new CandleEntry(50, high, low, open, close));
pos++;
} else if (pos == 1) {
yVals1.add(new CandleEntry(51, high, low, open, close));
pos++;
} else if (pos == 2) {
yVals1.add(new CandleEntry(52, high, low, open, close));
pos++;
} else if (pos == 3) {
yVals1.add(new CandleEntry(53, high, low, open, close));
pos++;
} else if (pos == 4) {
yVals1.add(new CandleEntry(54, high, low, open, close));
pos++;
}
mChart.invalidate();
}
@OnClick({R.id.btn_buy, R.id.btn_sell})
public void onGameButtonsClicked() {
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.addToBackStack(null);
String cashText = btnCashCurrency.getText().toString();
final int[] cash = {Integer.valueOf(cashText)};
if (cash[0] <= 0) {
restartGame.setVisibility(View.VISIBLE);
Toast.makeText(getContext(), "Your cash is on -, u cant play. Please restart game.", Toast.LENGTH_SHORT).show();
} else if (Integer.valueOf(spinnerData.getSelectedItem().toString()) >= Integer.valueOf(btnCashCurrency.getText().toString())) {
Toast.makeText(getContext(), "You not have available cash, change the money in Invest Section.", Toast.LENGTH_SHORT).show();
} else {
int min = 0;
int max = 2;
Random r = new Random();
int i1 = r.nextInt(max - min + 1) + min;
String text = spinnerData.getSelectedItem().toString();
final int temp = Integer.parseInt(text);
final int temp2 = temp * 2;
disableAndEnableButtons(false);
if (i1 == 0 || i1 == 1) {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
lostFragment.show(ft, DIALOG_LOST);
cash[0] -= temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "LOSE " + MainActivity.getUsername(getContext()).trim() + ": "
+ btnCashCurrency.getText().toString().trim() + " -" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
} else {
progress.setVisibility(View.VISIBLE);
stop();
configureHandler(900);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
winFragment.show(ft, DIALOG_WIN);
cash[0] += temp2;
btnCashCurrency.setText(cash[0] + "");
progress.setVisibility(View.GONE);
disableAndEnableButtons(true);
String score = "WIN " + MainActivity.getUsername(getContext()).trim()
+ ": " + btnCashCurrency.getText().toString().trim() + " +" + temp2;
HISTORYTRANSACTION.add(score);
getUserInfoAndSave();
stop();
configureHandler(5000);
}
}, 5000);
}
}
}
private void disableAndEnableButtons(boolean on) {
btnBuy.setEnabled(on);
btnSell.setEnabled(on);
}
@OnClick(R.id.restart_game)
public void restartGame() {
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
MainGameFragment.LEADERBOARDUSER.add(MainGameFragment.userNameAndScore);
((CurrencySelectActivity) getContext()).hideGameFragment();
((CurrencySelectActivity) getContext()).closeCurrencyActivity();
SharedPreferences prefs = getContext().getSharedPreferences("app.forex", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
Set<String> set = new HashSet<>();
set.add(String.valueOf(MainGameFragment.LEADERBOARDUSER));
edit.putStringSet("user_and_score", set);
HISTORYTRANSACTION.clear();
edit.apply();
}
private void configureSpinnerDataAndLogic() {
String[] arraySpinner = new String[]{
"50", "100", "150", "200", "250", "300", "400", "500"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(),
android.R.layout.simple_list_item_1, arraySpinner);
spinnerData.setAdapter(adapter);
spinnerData.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.WHITE);
String text = spinnerData.getSelectedItem().toString();
int temp = Integer.parseInt(text);
text_profit.setText((temp * 2) + " $ " + "100%");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
A及びここにあるXMLファイル
<com.github.mikephil.charting.charts.CandleStickChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/butonCurrency"
android:layout_margin="30dp" />
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/chartLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/butonCurrency"
android:layout_margin="30dp" />
そしてthisworkのような: 私は恵み開始高度助けを必要
を