java - 如何从 CandleStick 获取值并添加到 LineChart -> MPANDROIDCHART

标签 java android mpandroidchart

我在从 CandleStickChart 获取数据并将其添加到 LineChart 中时遇到问题。

好的,那么看看这个方法。我从 CandleStick 获取数据并将数据配置到 LineChart。一切都很完美。我已经生成了 2 个字符,就像你们在图片上看到的那样:

enter image description here

但问题是当我尝试将动态数据添加到字符时。我的方法仅将数据添加到 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);
    }

这里我添加了LineEntry,但这不起作用,我不知道为什么,你们有什么想法吗?

  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();
        }
    }

你们知道出了什么问题吗? 我开始悬赏: 这是完整的类(class):

 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) {
            }
        });
    }
}

这是 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" />

这个工作就像: enter image description here

非常需要帮助

最佳答案

您可以尝试使用 lineData.addEntry() 方法,而不是将新条目添加到条目数组中。

类似这样的事情:

private void addEntry() {
        LineData data = mChart.getData();
        ILineDataSet set = data.getDataSetByIndex(0);
        if (set == null) {
            set = createSet();
            data.addDataSet(set);
        }             
        data.addEntry(new Entry(data.getDataSetByIndex(dataSetIndex).getEntryCount(), yValue), dataSetIndex);
        data.notifyDataChanged();
        mChart.notifyDataSetChanged(); 

}

如果您的折线图中只有一个数据集,您的“dataSetIndex”将始终为 0。

欲了解更多信息,您可以查看此class

关于java - 如何从 CandleStick 获取值并添加到 LineChart -> MPANDROIDCHART,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43278076/

相关文章:

android - MPAndroidChart 在缩放时更改数据值

java - 添加两个 Optional<BigDecimal> 数字的最优雅方法是什么

java - 某些 JVM 是否会缓存常用库类的静态初始化结果

android - 在来电时重启 Android 应用程序

Android texSubImage2D - 在不同的设备上有不同的行为吗?

android - 添加行数据时出现 IllegalArgumentException

java - 如何给ImageButton添加高度?

java - token 语法错误,预期 ConstructorHeaderName 和 token 语法错误 "(",< 预期

android - 在android中以编程方式设置 fragment 参数

android - MPAndroidChart:从右开始的实时线图