Browse Source

configure bar chart

pull/55/head
Thodoris1999 6 years ago
parent
commit
d4d501ba5c
  1. 32
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 5
      app/src/main/java/gr/thmmy/mthmmy/model/Poll.java
  3. 68
      app/src/main/res/layout/activity_topic_poll.xml

32
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -40,13 +40,11 @@ import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import com.github.mikephil.charting.charts.HorizontalBarChart; import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.AxisBase; import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis; import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData; import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet; import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry; import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.squareup.picasso.Picasso; import com.squareup.picasso.Picasso;
import java.util.ArrayList; import java.util.ArrayList;
@ -188,18 +186,36 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.rootLayout.addView(radioGroup, 1); holder.rootLayout.addView(radioGroup, 1);
} else { } else {
//Showing results //Showing results
ArrayList<BarEntry> valuesToCompare = new ArrayList<>(); List<BarEntry> valuesToCompare = new ArrayList<>();
for (int i = 0; i < entries.length; i++) { for (int i = 0; i < entries.length; i++) {
valuesToCompare.add(new BarEntry(entries[i].getVotes(), i)); valuesToCompare.add(new BarEntry(i, entries[i].getVotes()));
} }
BarDataSet data = new BarDataSet(valuesToCompare, "Vote Results"); BarDataSet data = new BarDataSet(valuesToCompare, "Vote Results");
YAxis yAxisLeft = holder.voteChart.getAxisLeft(); YAxis yAxisLeft = holder.voteChart.getAxisLeft();
yAxisLeft.setValueFormatter((value, axis) -> entries[(int) value].getEntryName()); yAxisLeft.setGranularity(1f);
YAxis yAxisRight = holder.voteChart.getAxisRight();
yAxisRight.setEnabled(false);
XAxis xAxis = holder.voteChart.getXAxis();
xAxis.setValueFormatter((value, axis) -> entries[(int) value].getEntryName());
xAxis.setTextColor(context.getResources().getColor(R.color.primary_text));
xAxis.setGranularity(1f);
xAxis.setDrawGridLines(false);
xAxis.setDrawAxisLine(false);
xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
BarData barData = new BarData(data); BarData barData = new BarData(data);
barData.setValueTextColor(context.getResources().getColor(R.color.accent));
holder.voteChart.setData(barData); holder.voteChart.setData(barData);
holder.voteChart.getLegend().setEnabled(false);
holder.voteChart.getDescription().setEnabled(false);
holder.voteChart.invalidate();
} }
if (poll.getRemoveVoteUrl() != null) holder.removeVotesButton.setVisibility(View.VISIBLE);
if (poll.getShowVoteResultsUrl() != null) holder.showPollResultsButton.setVisibility(View.VISIBLE);
if (poll.getShowOptionsUrl() != null) holder.showPollOptionsButton.setVisibility(View.VISIBLE);
if (poll.getPollFormUrl() != null) holder.submitButton.setVisibility(View.VISIBLE);
} else { } else {
Post currentPost = (Post) topicItems.get(position); Post currentPost = (Post) topicItems.get(position);
if (currentHolder instanceof PostViewHolder) { if (currentHolder instanceof PostViewHolder) {
@ -702,7 +718,7 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
final TextView question, errorTooManySelected; final TextView question, errorTooManySelected;
final LinearLayout rootLayout; final LinearLayout rootLayout;
final AppCompatButton submitButton; final AppCompatButton submitButton;
final AppCompatButton removeVotesButton; final AppCompatButton removeVotesButton, showPollResultsButton, showPollOptionsButton;
final HorizontalBarChart voteChart; final HorizontalBarChart voteChart;
public PollViewHolder(View itemView) { public PollViewHolder(View itemView) {
@ -712,6 +728,8 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
rootLayout = (LinearLayout) itemView; rootLayout = (LinearLayout) itemView;
submitButton = itemView.findViewById(R.id.submit_button); submitButton = itemView.findViewById(R.id.submit_button);
removeVotesButton = itemView.findViewById(R.id.remove_vote_button); removeVotesButton = itemView.findViewById(R.id.remove_vote_button);
showPollResultsButton = itemView.findViewById(R.id.show_poll_results_button);
showPollOptionsButton = itemView.findViewById(R.id.show_poll_options_button);
errorTooManySelected = itemView.findViewById(R.id.error_too_many_checked); errorTooManySelected = itemView.findViewById(R.id.error_too_many_checked);
voteChart = itemView.findViewById(R.id.vote_chart); voteChart = itemView.findViewById(R.id.vote_chart);
} }

5
app/src/main/java/gr/thmmy/mthmmy/model/Poll.java

@ -99,5 +99,10 @@ public class Poll extends TopicItem {
public void setVotes(int votes) { public void setVotes(int votes) {
this.votes = votes; this.votes = votes;
} }
@Override
public String toString() {
return "Vote label:" + entryName + ", num votes:" + votes;
}
} }
} }

68
app/src/main/res/layout/activity_topic_poll.xml

@ -1,20 +1,21 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout" android:id="@+id/rootLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView <TextView
android:id="@+id/question_textview" android:id="@+id/question_textview"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" /> android:layout_height="wrap_content"
android:textColor="@color/primary_text" />
<com.github.mikephil.charting.charts.HorizontalBarChart <com.github.mikephil.charting.charts.HorizontalBarChart
android:id="@+id/vote_chart" android:id="@+id/vote_chart"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="250dp" /> android:layout_height="200dp" />
<TextView <TextView
android:id="@+id/error_too_many_checked" android:id="@+id/error_too_many_checked"
@ -22,25 +23,44 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/error_too_many_checked" android:text="@string/error_too_many_checked"
android:textColor="@color/red" android:textColor="@color/red"
android:visibility="gone"/> android:visibility="gone" />
<android.support.v7.widget.AppCompatButton <!-- controls -->
android:id="@+id/submit_button" <LinearLayout
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:text="@string/submit"/>
<android.support.v7.widget.AppCompatButton <View
android:id="@+id/remove_vote_button" android:layout_width="0dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"/>
android:text="@string/remove_vote_button"
android:visibility="gone"/>
<android.support.v7.widget.AppCompatButton <android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_results_button" android:id="@+id/remove_vote_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/show_vote_results_button" android:text="@string/remove_vote_button"
android:visibility="gone"/> android:visibility="gone" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_results_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_vote_results_button"
android:visibility="gone" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/show_poll_options_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_vote_results_button"
android:visibility="gone" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/submit"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
Loading…
Cancel
Save