如何通过 MPandroidChart 在我的烛图上添加高亮文本?
How to add highlight text on my candlechart by MPandroidChart?
enter image description here
我只想在图表中添加 "To show a small square around the highlight crosshair, which shows the data from the Y-axis when I swipe on the chart",
例如,如果我使用折线图
入口数据由以下for循环构造
for (int i = 0; i <= barcount; i ++) {
LinValues.add (new Entry (i, 10000 + 2 * i));
}
///Set LineDataSet
LineDataSet setline = new LineDataSet (LinValues, "AVER");
setline.setAxisDependency (YAxis.AxisDependency.LEFT);
setline.setColor (Color.GREEN);
setline.setLineWidth (1f);
setline.setDrawCircles (false);
setline.setDrawValues (false);
setline.setHighLightColor (Color.WHITE);
LineData line = new LineData (setline);
Legend l =combchart.getLegend();
l.setTextColor(Color.WHITE);
YAxis yLAxis =combchart.getAxisLeft();
yLAxis.setTextSize(22f);
yLAxis.setLabelCount(5,true);
yLAxis.setTextColor(Color.WHITE);
YAxis yRAxis = combchart.getAxisRight();
yRAxis.setEnabled(false);
Linechat.setData (line);
如何将"LinValues"中的"ith value"添加到第i个十字准线旁边,当我滑动到"nth data point"时,十字准线附近的值将变为"nth value"来自“LinValues?
你能帮帮我吗?????
好的,这可以按照以下说明使用标记视图来完成:
首先在你的项目中添加java class CustomMarkerView 并在其中编写以下代码。
package maniac.professionalchartsfree.Utilities;
import android.content.Context;
import android.widget.TextView;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import maniac.professionalchartsfree.R;
/**
* Created by saad.rafique on 1/5/2018.
*/
public class CustomMarkerView extends MarkerView
{
private TextView tvContent;
public CustomMarkerView(Context context, int layoutResource, int valueSize, int valueColor)
{
super(context, layoutResource);
// this markerview only displays a textview
tvContent = findViewById(R.id.tvContent);
tvContent.setTextSize(valueSize);
tvContent.setTextColor(valueColor);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight)
{
tvContent.setText("x: " + e.getX() + "\n" +"y: " + e.getY()); // set the entry-value as the display text
}
}
之后在布局文件夹中创建一个 custom_marker.xml 文件并在其中写入以下代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/markers_bg"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="15dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
在您创建图表的 activity 添加方法之后:
public CustomMarkerView markerView(Context context)
{
CustomMarkerView mv = new CustomMarkerView(context, R.layout.custom_marker, 16, Color.WHITE);
mv.setOffset(- mv.getWidth() / 2, -mv.getHeight()-25);
return mv;
}
之后添加以下行:
lineChart.setDrawMarkers(true);
lineChart.setMarker(markerView(context));
现在 运行 你的项目,当你点击折线图的点时,你会得到你想要的。
enter image description here 我只想在图表中添加 "To show a small square around the highlight crosshair, which shows the data from the Y-axis when I swipe on the chart", 例如,如果我使用折线图 入口数据由以下for循环构造
for (int i = 0; i <= barcount; i ++) {
LinValues.add (new Entry (i, 10000 + 2 * i));
}
///Set LineDataSet
LineDataSet setline = new LineDataSet (LinValues, "AVER");
setline.setAxisDependency (YAxis.AxisDependency.LEFT);
setline.setColor (Color.GREEN);
setline.setLineWidth (1f);
setline.setDrawCircles (false);
setline.setDrawValues (false);
setline.setHighLightColor (Color.WHITE);
LineData line = new LineData (setline);
Legend l =combchart.getLegend();
l.setTextColor(Color.WHITE);
YAxis yLAxis =combchart.getAxisLeft();
yLAxis.setTextSize(22f);
yLAxis.setLabelCount(5,true);
yLAxis.setTextColor(Color.WHITE);
YAxis yRAxis = combchart.getAxisRight();
yRAxis.setEnabled(false);
Linechat.setData (line);
如何将"LinValues"中的"ith value"添加到第i个十字准线旁边,当我滑动到"nth data point"时,十字准线附近的值将变为"nth value"来自“LinValues? 你能帮帮我吗?????
好的,这可以按照以下说明使用标记视图来完成:
首先在你的项目中添加java class CustomMarkerView 并在其中编写以下代码。
package maniac.professionalchartsfree.Utilities;
import android.content.Context;
import android.widget.TextView;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.highlight.Highlight;
import maniac.professionalchartsfree.R;
/**
* Created by saad.rafique on 1/5/2018.
*/
public class CustomMarkerView extends MarkerView
{
private TextView tvContent;
public CustomMarkerView(Context context, int layoutResource, int valueSize, int valueColor)
{
super(context, layoutResource);
// this markerview only displays a textview
tvContent = findViewById(R.id.tvContent);
tvContent.setTextSize(valueSize);
tvContent.setTextColor(valueColor);
}
// callbacks everytime the MarkerView is redrawn, can be used to update the
// content (user-interface)
@Override
public void refreshContent(Entry e, Highlight highlight)
{
tvContent.setText("x: " + e.getX() + "\n" +"y: " + e.getY()); // set the entry-value as the display text
}
}
之后在布局文件夹中创建一个 custom_marker.xml 文件并在其中写入以下代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="75dp"
android:layout_height="75dp"
android:background="@drawable/markers_bg"
>
<TextView
android:id="@+id/tvContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="15dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
在您创建图表的 activity 添加方法之后:
public CustomMarkerView markerView(Context context)
{
CustomMarkerView mv = new CustomMarkerView(context, R.layout.custom_marker, 16, Color.WHITE);
mv.setOffset(- mv.getWidth() / 2, -mv.getHeight()-25);
return mv;
}
之后添加以下行:
lineChart.setDrawMarkers(true);
lineChart.setMarker(markerView(context));
现在 运行 你的项目,当你点击折线图的点时,你会得到你想要的。