Set bar width on ColumnChart from google chart in Java - java

I am having trouble setting the bar width on my bars in my ColumnChart.
So far I have the following options set:
private Options createOptions() {
Options options = Options.create();
options.setWidth(600);
options.setHeight(400);
options.set3D(true);
options.setEnableTooltip(false);
return options;
}
But it seems like the option for setting the bar width is not there. If I set my width to 600 px, and only have 2 entries/bars, they will each be huge. If I have 10 it will look more normal.

I found the documentation for this here: https://developers.google.com/chart/interactive/docs/gallery/barchart
The variable you'll want to be modifying is bar.groupWidth - you can set it to a size, or a percent ratio. So possibly something like this:
bar: {
groupWidth: 20
}
See some other resources if you need more help:
Google Charts / visualisations column width
Here's another great set of posts:
http://developer.actuate.com/community/forum/index.php?/topic/28797-bar-charts-bar-width/

Related

Vaadin Pie chart takes a strange form when none of the legend labels is selected

I am using Vaadin framework with Spring boot and I need to display a PIE Chart with its contents. The thing is everything is working fine and when I click the legends labels the animation works fine.
Pre-click:
When I click the labels:
Note that when I clicked the labels the pie changed, which is what I want.
But when I click all the labels:
The pie chart turns into this blue I don't even know what this is.
I was wondering if anyone knows how would I access the labels in the legends to check if they are selected via Java and if none of them is selected I wouldn't let the user deselect the last one.
Thank you for your help
Find the chart code here:
private Component getCompaniesChart() {
Chart chart = new Chart(ChartType.PIE);
Configuration conf = chart.getConfiguration();
DataSeries dataSeries = new DataSeries();
service.findAllDepartments()
.forEach(Department -> dataSeries.add(new DataSeriesItem(Department.getName(), Department.getEmployeeCount())));
Options3d options3d = new Options3d();
options3d.setEnabled(true);
options3d.setAlpha(70);
options3d.setBeta(0);
conf.getChart().setOptions3d(options3d);
PlotOptionsPie plotOptions = new PlotOptionsPie();
plotOptions.setDepth(100);
plotOptions.setAllowPointSelect(true);
plotOptions.setShowInLegend(true);
plotOptions.setShadow(true);
plotOptions.setSize("200%");
conf.getLegend().setLabelFormat("{name} ({y})");
conf.getLegend().setItemMarginTop(100);
conf.setPlotOptions(plotOptions);
conf.setSeries(dataSeries);
conf.getChart().setStyledMode(true);
return chart;
}

How to get Material3 primary/accent colors programmatically?

Google introduced Material 3 which allows the user to choose a color to apply for the whole system theme, but their documentation isn't clear and didn't mention how to get current colors programmatically to use for the app such as changing a view's background color or text color.
E.g.: view.setBackgroundColor(Material3.getPrimaryColor());
Of course Material3.getPrimaryColor() doesn't exist and it is just an example of what I need.
Any help is appreciated, thanks.
First - bear in mind support for dynamic theming was added in Android 12 (API 31) and not all manufacturers support it yet, much less a compatibility implementation for lower versions.
Here is the documentation on how to use dynamic colors in general, including theme overlay and activity color overlay.
If you want to create themed views it's easier to use appropriate DynamicColor theme or at least wrapped context to inflate them and let them get stylized accordingly.
To just get specific colors you need to use last step - wrap a context with DynamicColors theme:
if (DynamicColors.isDynamicColorAvailable()) {
// if your base context is already using Material3 theme you can omit R.style argument
Context dynamicColorContext = DynamicColors.wrapContextIfAvailable(context, R.style.ThemeOverlay_Material3_DynamicColors_DayNight);
// define attributes to resolve in an array
int[] attrsToResolve = {
R.attr.colorPrimary, // 0
R.attr.colorOnPrimary, // 1
R.attr.colorSecondary, // 2
R.attr.colorAccent // 3
};
// now resolve them
TypedArray ta = dynamicColorContext.obtainStyledAttributes(attrsToResolve);
int primary = ta.getColor(0, 0);
int onPrimary = ta.getColor(1, 0);
int secondary = ta.getColor(2, 0);
int accent = ta.getColor(3, 0);
ta.recycle(); // recycle TypedArray
// here you can consume dynamic colors
}

How to toggle fullscreen borderless on multiple monitors properly

I was looking for a way to properly choose the monitor I want to fullscreen on based on the position of the window prior to fullscreening.
I looked online for ages but couldn't find anything so I ended up trying a bunch of things until I got something working.
I figured someone eventually will try to look this problem up and I might as well share my solution.
What I ended up doing is get the virtual screen coordinates of the center of the window using LWJGL2's Display class like so:
int x = Display.getX() + Display.getWidth()/2,
y = Display.getY() + Display.getHeight()/2;
I then used AWT to get all available monitors:
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()
I iterated through them and got their virtual bounds(and applied any DPI scaling they might have):
Rectangle bounds = screenDevice.getDefaultConfiguration().getDefaultTransform().createTransformedShape(screenDevice.getDefaultConfiguration().getBounds()).getBounds();
EDIT: Slightly altered line so it can support Windows' DPI scaling properly.
If the bounds contained the center of the window, it means that most of the window is probably within that monitor:
if(bounds.contains(x,y))
return bounds; //getMonitorFromWindow()
Then to toggle between windowed borderless fullscreen and normal windowed in LibGDX I did the following:
// config is the LwjglApplicationConfiguration of the application
// upon changing using alt+enter
if(fullscreen) {
config.resizable = false;
Gdx.graphics.setUndecorated(true);
Rectangle monitor = getMonitorFromWindow();
// set to full screen in current monitor
Gdx.graphics.setWindowedMode(monitor.width, monitor.height);
Display.setLocation(monitor.x, monitor.y);
} else {
config.resizable = true;
Gdx.graphics.setUndecorated(false);
Rectangle monitor = getMonitorFromWindow();
// set to windowed centered in current monitor
Gdx.graphics.setWindowedMode((int) (monitor.width * 0.8f), (int) (monitor.height * 0.8f));
Display.setLocation(monitor.x + (int) (monitor.width * 0.1f), monitor.y + (int) (monitor.height * 0.1f));
}
I hope someone would find this useful.

How to get image dimensions in Magnolia CMS

I have an absolute positioned image whose parent container I need to set a height on, but now I can't find a method in magnolia API that allows me to get the width or height of an asset/item.
Is there a way to get Image Dimensions (height, width) of an Asset/Item in Magnolia CMS using only the native API?
If not, how would you do it?
I would write it somehow like
import info.magnolia.dam.api.metadata.MagnoliaAssetMetadata;
// ...
if (asset.supports(MagnoliaAssetMetadata.class)) {
MagnoliaAssetMetadata metadata = asset.getMetadata(MagnoliaAssetMetadata.class);
long width = metadata.getWidth();
long height = metadata.getHeight();
// do whatever you need with the image dimensions
} else {
// handle non-image asset
}
See MagnoliaAssetMetadata javadoc and/or sources (and related classes) for more info.
I have done something like this, base on what you have suggested. I am using damTemplatingFunctions to get the asset using the JCR id.
Node node = nodes.nextNode();
String imageJcrId = node.getProperty("image").getValue().getString();
Asset asset = damTemplatingFunctions.getAsset(imageJcrId);
MagnoliaAssetMetadata metadata1 = asset.getMetadata(MagnoliaAssetMetadata.class);
System.out.println(metadata1.getHeight());
System.out.println(metadata1.getWidth());

Libgdx Table too small, does not fill viewport

I'm trying to use a Table to create a menu with lined-up Buttons and Labels. I'm using the latest nightly builds of libgdx, which changed the Table API (amongst other things).
Here's the code in my Screen constructor:
this.mStage = new Stage();
this.mTable = new Table();
this.mTable.setFillParent(true);
this.mTable.debugTable();
this.mStage.addActor(this.mTable);
// now I add some stuff to the table
// ...
My resize function looks like this:
this.mStage.setViewport(width, height, true);
this.mTable.setFillParent(true);
this.mTable.invalidate();
My render function has the following:
System.out.println("Table size is " +
this.mTable.getWidth() + " by " +
this.mTable.getHeight());
this.mStage.draw();
Table.drawDebug();
Although my console shows the correct size:
Table size is 480.0 by 640.0
the Table size is shrink-wrapped around its children, and does not extend to the correct size of 480x640.
Any ideas?
Answering my own question for the benefit of others. The way I was adding widgets to the table was incorrect:
this.mTable.row();
this.mTable.add(button).fillX();
I needed to call the fillX() method of the row():
this.mTable.row().fillX();
this.mTable.add(button);
Hope this helps someone.
To add to the above answers, it may be helpful to know that you can make these setting defaults for the table like so:
this.mTable.defaults().expandX().fillX();
The settings will apply uniformly to all rows and cells.
Your incorrect version of adding widget to the table can be easily
corrected by expanding a cell first using .expandX() method and then
by filling that cell with a widget by calling fillX().
so your initial code:
this.mTable.row();
this.mTable.add(button).fillX();
should be corrected to the following version:
this.mTable.row();
this.mTable.add(button).expandX().fillX();
these 2 lines can be shorten to one:
this.mTable.add(button).expandX().fillX().row();

Categories