I've added a GraphView object and populated it with some data as per the example in the documentation on the website. While I've found out how to change the background colour of the GraphView, I have no idea how to change the grid colour. Any ideas?
This is what I've tried:
public void createGraph(View view){
GraphView graph = (GraphView) view.findViewById(R.id.graph);
GridLabelRenderer gridLabelRenderer = graph.getGridLabelRenderer();
// This works
graph.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
// This does not work
gridLabelRenderer.setGridColor(getResources().getColor(android.R.color.holo_green_light));
// Nor does this
//gridLabelRenderer.setGridColor(15);
// This works
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
}
try
graph.getGridLabelRenderer().reloadStyles();
styling example it here
https://github.com/appsthatmatter/GraphView-Demos/blob/master/app/src/main/java/com/jjoe64/graphview_demos/examples/StylingColors.java
Related
I have two classes, the first contains the existing GUI and the second is for creating a line chart.
Now my question is how can I include the created Chart in my existing GUI without creating a new Window like it is now?
Chart Creating Method:
public void start(Stage s) throws Exception {
s.setTitle("JavaFX Realtime Chart Demo");
//defining the axes
final CategoryAxis xAxis = new CategoryAxis(); // we are gonna plot against time
final NumberAxis yAxis = new NumberAxis(0,10,1);
xAxis.setLabel("Time/s");
xAxis.setAnimated(false); // axis animations are removed
yAxis.setLabel("Value");
yAxis.setAnimated(false); // axis animations are removed
//creating the line chart with two axis created above
final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
lineChart.setTitle("Realtime JavaFX Charts");
lineChart.setAnimated(true); // disable animations
//defining a series to display data
XYChart.Series<String, Number> series = new XYChart.Series<>();
series.setName("U/s");
XYChart.Series<String, Number> series2 = new XYChart.Series<>();
series2.setName("km/h");
// add series to chart
lineChart.getData().addAll(series,series2);
// setup scene
Scene scene = new Scene(lineChart, 800, 600);
s.setScene(scene);
// show the stage
s.show();
// this is used to display time in HH:mm:ss format
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
// setup a scheduled executor to periodically put data into the chart
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
// put dummy data onto graph per second
scheduledExecutorService.scheduleAtFixedRate(() -> {
// get a random integer between 0-10
wert = G.gebeInt();
wert_2 = G.gebeV();
// Update the chart
Platform.runLater(() -> {
// get current time
Date now = new Date();
// put random number with current time
series.getData().add(new XYChart.Data<>(simpleDateFormat.format(now), wert));
series2.getData().add(new XYChart.Data<>(simpleDateFormat.format(now), wert_2));
if (series.getData().size() > WINDOW_SIZE) {
series.getData().remove(0);
}
if (series2.getData().size() > WINDOW_SIZE) {
series2.getData().remove(0);
}
});
}, 0, 2, TimeUnit.SECONDS);
}
I use Swing for the GUI and JavaFX for creating the charts, in this case a LineChart.
I am developing an Application in Android Studio to that prints how many of each item you can buy with the given amount of currency. It printed flawlessly when run as a Java program in Eclipse but I can not get it to print more than one line in the TextView Box.
I've noticed it will pick the most Expensive item you can afford 1 of and print it alone, leading me to believe it runs through the list and only prints the last one that passes as affordable. I've read about needing to use a StringBuilder and such but have found little information on how to convert my Array.asList over to this. Here is my code.
gCalc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText diamondInput = (EditText) findViewById(R.id.diamondInput);
try {
int diamonds = Integer.parseInt(diamondInput.getText().toString());
List<Gifts> gift = Arrays.asList(new Gifts[]{new Gifts("Gold star", 10), new Gifts("Love Bear", 10), new Gifts("Lillies", 10), new Gifts("Box Of Chocolate", 20), new Gifts("Taco", 20), new Gifts("Thumbs Up", 30), new Gifts("Panda", 40), new Gifts("Beer", 40), new Gifts("Patriot", 52), new Gifts("Eagle", 52), new Gifts("Gold Chain", 80), new Gifts("Roses", 100), new Gifts("Champagne", 100), new Gifts("Snow", 100), new Gifts("Candy", 100), new Gifts("Kiss", 200), new Gifts("Candy Hearts", 250), new Gifts("Peach", 300), new Gifts("EggPlant", 300), new Gifts("Fireworks", 500), new Gifts("GemDrop", 600), new Gifts("Crown", 600), new Gifts("Cupcakes", 700), new Gifts("Heart Balloon", 800), new Gifts("Sports Car", 1000), new Gifts("Smoke Rings", 1000), new Gifts("purple Diamond", 2500), new Gifts("Cupid", 5000), new Gifts("Gold Watch", 5000), new Gifts("Castle", 5000), new Gifts("Yacht", 10000), new Gifts("Jet", 20000)});
double coins = (double) diamonds / 2.5D;
Iterator var5 = gift.iterator();
while(var5.hasNext()) {
Gifts Gifts = (Gifts)var5.next();
int qty = (int)Gifts.getQty(coins);
if(qty > 0) {
result.setText("You can buy " + qty + " " + Gifts.name);
}
}
}
catch (Exception e) {
// friendly error to the user: field is incorrect
}
I need it to print as this EX:
You can buy X amount of Y
You can buy X amount of Y
You can buy X amount of Y
:end
Printing every item that can be bought and it's quantity.
If you look into Android API documentation (https://developer.android.com/reference/android/widget/TextView.html) , this is what it states:
void setText (CharSequence text) - sets the text to be displayed. - this means that each time you call this method, new text overrides the old one.
It seems that you are looking for append() method:
void append(CharSequence text) - convenience method to append the specified text to the TextView's display buffer, upgrading it to EDITABLE if it was not already editable. append() method doesn't override previously set text.
Another way to append text to the previously stored in TextView is to combine these methods:
result.setText(result.getText() + "text that you want to append to the previous one")
I have a Barchart with 4 bars which are grouped into 2 groups using MPAndroidChart library.
This works so far. Now i want to display a title above each group. This is my current output (as you can see, the titles are not showed above each group).
My code:
_overviewBarChart.setPinchZoom(false);
_overviewBarChart.getDescription().setEnabled(false);
_overviewBarChart.setDrawValueAboveBar(true);
_overviewBarChart.getXAxis().setCenterAxisLabels(true);
_overviewBarChart.getXAxis().setAxisMinimum(0);
_overviewBarChart.getXAxis().setDrawGridLines(false);
private void updateOverviewBarChart() {
//Init calorie needs bars
BarEntry todayCalorieNeedsBarEntry = new BarEntry(0, _calorieEntry.getTarget());
BarEntry yesterdayCalorieNeedsBarEntry = new BarEntry(0, 0);
if (_yesterdayCalorieEntry != null)
yesterdayCalorieNeedsBarEntry = new BarEntry(0, _yesterdayCalorieEntry.getTarget());
ArrayList<BarEntry> calorieNeedsBarEntries = new ArrayList<>();
calorieNeedsBarEntries.add(todayCalorieNeedsBarEntry);
calorieNeedsBarEntries.add(yesterdayCalorieNeedsBarEntry);
//Init consumed calories bars
BarEntry todayConsumedCaloriesBarEntry = new BarEntry(1, _calorieEntry.getConsumed());
BarEntry yesterdayConsumedCaloriesBarEntry = new BarEntry(1, 0);
if (_yesterdayCalorieEntry != null)
yesterdayConsumedCaloriesBarEntry = new BarEntry(1, _yesterdayCalorieEntry.getConsumed());
ArrayList<BarEntry> consumedCaloriesBarEntries = new ArrayList<>();
consumedCaloriesBarEntries.add(todayConsumedCaloriesBarEntry);
consumedCaloriesBarEntries.add(yesterdayConsumedCaloriesBarEntry);
//Init BarDataSets
BarDataSet calorieNeedsBarDataSet = new BarDataSet(calorieNeedsBarEntries, getString(R.string.fragment_main_calorieneeds));
calorieNeedsBarDataSet.setColor(Color.parseColor("#26A69A"));
BarDataSet consumedCaloriesBarDataSet = new BarDataSet(consumedCaloriesBarEntries, getString(R.string.fragment_main_consumed));
consumedCaloriesBarDataSet.setColor(Color.parseColor("#E53935"));
//Init BarData, group BarEntrys, set group titles
BarData barData = new BarData(calorieNeedsBarDataSet, consumedCaloriesBarDataSet);
barData.setValueTextSize(14);
barData.setBarWidth(0.2f);
barData.groupBars(0, 0.15f, 0.1f);
ArrayList<String> groupTitles = new ArrayList<String>();
groupTitles.add(getString(R.string.fragment_main_today));
groupTitles.add(getString(R.string.fragment_main_yesterday));
_overviewBarChart.getXAxis().setValueFormatter(new IndexAxisValueFormatter(groupTitles));// new BarChartStringFormatter(groupTitles));
//Set data, redraw BarChart
_overviewBarChart.setData(barData);
_overviewBarChart.invalidate();
}
Can you see what i'm doing wrong?
Finally i found the solution for my problem.
First you have to add this two lines of code (note: The parameters can vary for your requirements):
_overviewBarChart.getXAxis().setGranularity(1f);
_overviewBarChart.getXAxis().setAxisMaximum(2);
My mistake was that the default XAxis is to finely granulated (0.1, 0.2, 0.3 ...). So IndexAxisValueFormatter applies the group title to each XAxis step. setGranularity resolves this. Finally setAxisMaximum is used to restrict the amount of steps on the XAxis (previous was 3 so the title showed 3 times).
And then change...
barData.groupBars(0, 0.15f, 0.1f);
to this (note: The parameters can vary for your requirements)..
barData.groupBars(0, 0.45f, 0.1f);
Result:
I have the following code which is a grouping of buttons in javafx:
ToggleGroup groupLevelQ = new ToggleGroup();
class MyLevelButton extends ToggleButton {
public MyLevelButton(String name) {
super(name);
setPrefWidth(50.0);
setPrefHeight(50.0);
setStyle("-fx-font: 20 cornerstone; -fx-base: #17499F;");
setToggleGroup(groupLevelQ);
}
}
oneLevelButton = new MyLevelButton("1");
twoLevelButton = new MyLevelButton("2");
threeLevelButton = new MyLevelButton("3");
fourLevelButton = new MyLevelButton("4");
fiveLevelButton = new MyLevelButton("5");
sixLevelButton = new MyLevelButton("6");
sevenLevelButton = new MyLevelButton("7");
eightLevelButton = new MyLevelButton("8");
nineLevelButton = new MyLevelButton("9");
oneLevelButton.setUserData("1");
twoLevelButton.setUserData("2");
threeLevelButton.setUserData("3");
fourLevelButton.setUserData("4");
fiveLevelButton.setUserData("5");
sixLevelButton.setUserData("6");
sevenLevelButton.setUserData("7");
eightLevelButton.setUserData("8");
nineLevelButton.setUserData("9");
groupLevelQ.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
public void changed(ObservableValue<? extends Toggle> ov, Toggle toggle, Toggle new_toggle) {
if (new_toggle != null) {
textLevelQ = (String) groupLevelQ.getSelectedToggle().getUserData();
}
}
});
addQButtonPane.add(oneLevelButton, 1, 9);
addQButtonPane.add(twoLevelButton, 2, 9);
addQButtonPane.add(threeLevelButton, 3, 9);
addQButtonPane.add(fourLevelButton, 4, 9);
addQButtonPane.add(fiveLevelButton, 5, 9);
addQButtonPane.add(sixLevelButton, 6, 9);
addQButtonPane.add(sevenLevelButton, 7, 9);
addQButtonPane.add(eightLevelButton, 8, 9);
addQButtonPane.add(nineLevelButton, 9, 9);
In fact I am creating buttons and I add them in a GridPane. I am trying to figure out how can I define the distance between the buttons. Basically as they are right now they have a default distance them and I want to change that.
EDIT:
addQButtonPane = new GridPane();
addQButtonPane.setHgap(10);
addQButtonPane.setVgap(10);
addQButtonPane.setPadding(new Insets(0, 50, 0, 50));
addQButtonPane.setStyle("-fx-background-color: #95CBE5;");
This is the way that my gridpane is formatted. But still I want to change this formation just for the specific mentioned buttons.
EDIT2:
Maybe the kind of layout you're trying to achieve could be better achieved by placing HBoxes containing the options and Text elements inside a VBox.
But if you're looking for a way to "take the buttons out of the usual layout", you could simply place them inside a HBox use a columnSpan that covers all remaining columns:
double buttonDistance = ...
int gridPaneColumnCount = ...
HBox buttonBox = new HBox(buttonDistance,
oneLevelButton,
twoLevelButton,
threeLevelButton,
fourLevelButton,
fiveLevelButton,
sixLevelButton,
sevenLevelButton,
eightLevelButton,
nineLevelButton);
addQButtonPane.add(buttonBox, 1, 9, gridPaneColumnCount-1, 1);
I'm having issues getting libgdxs scrollpane control to work. The code below shows control setup for a simple layout with a label, a List of items inside a scrollpane, and a button. The problem is my scroll pane doesn't show anything other than the vScroll/vScrollKnob ninepatch (that tiny white square)
it just looks like this:
screenshot.
private void setupLayout()
{
String[] listEntries = {"1","2","3","4","5"};
ListStyle listStyle = new ListStyle();
NinePatch example = new NinePatch(new Texture(Gdx.files.internal("data/example.9.png")));
listStyle.selectedPatch = example;
listStyle.font = new BitmapFont();
mList = new List(listEntries,listStyle);
ScrollPaneStyle paneStyle = new ScrollPaneStyle();
paneStyle.vScroll = example;
paneStyle.vScrollKnob = example;
mListScroll = new ScrollPane(mList,paneStyle);
mListScroll.setScrollingDisabled(true, false);
mListScroll.width = 500;
mListScroll.height = 500;
LabelStyle ls = new LabelStyle();
ls.font = new BitmapFont();
ls.fontColor = new Color(1.0f, 1.0f, 1.0f, 1.0f);
mLabel = new Label("Label", ls);
TextButtonStyle buttonStyle = new TextButtonStyle();
buttonStyle.font = new BitmapFont();
mButton = new TextButton("Button",buttonStyle);
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mListScroll);
mStage.addActor(table);
}
It works as expected if i don't user the scroll pane and add the list directly to the table like this:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
table.add(mList); //changed mListScroll(scrollpane class) to mList(List class)
mStage.addActor(table);
screenshot
But then it will extend out the bottom of my screen when there are too many items. What am I doing wrong here? Is there another parameter i need to set on the scrollpane?
I believe the issue you are having is how you are adding things to the table. I would suggest the following code instead of how you are doing it:
Table table = new Table();
table.add(mLabel);
table.row();
table.add(mButton);
table.row();
// Changing the table layout size itself.
table.add(mListScroll).size(500, 500);
mStage.addActor(table);
For more a more detailed explanation refer to TableLayout the quick start here shows you more how using tables for laying out objects.