JFreeChart Bar Charts - Draw plot without gradient fill - colors

My Bar chart is always drawn with a gradient fill. I have a code to fill each bar in a series with a different color. I just want the colors without any effect. I looked at a previous thread but that did not help since it seems to be designed for a default standard code.
Here is my code:
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.WHITE);
CategoryItemRenderer renderer = new MyRenderer();
plot.setRenderer(renderer);
class MyRenderer extends BarRenderer
{
private Paint[] barColors;
public MyRenderer()
{
this.barColors = new Paint[] { new Color( 21, 104, 156), new Color( 25, 149, 104), new Color( 237, 179,20),
new Color( 72, 181, 163) };
}
public Paint getItemPaint(final int row, final int column)
{
// returns color for each column
return (this.barColors[column % this.barColors.length]);
}
}
Any help will be appreciated.
Thanks.

In the BarRenderer class you will find this method:
public static void setDefaultBarPainter(BarPainter painter);
It allows you to change the default bar painter (pass a StandardBarPainter to this method before creating any charts and your charts will have bars without the gradient).

Related

How to measure ImageView before creating bitmap into it?

My ImageView is matching screen size on x-axis and is using remaining space on y-axis in my layout. I want to create bitmap into this ImageView with exactly the same size as the ImageView is. How to make it please? Can it be done by some automatic setting, should I call some measure function?
I tried SetAdjustViewBounds() but it didn't work for me.
Creating Bitmap big enough (I don't like much such a memory wasting) and setting SetScaleType(ImageView.ScaleType.Matrix) works, but still when I'm making drawing operations on canvas, I don't know real size of area I should paint into, both canvas and bitmap height are equal to yScreen while imgWeekView height is pretending to be 0, even though it paints whole desired area with gray color.
imgWeekView = new ImageView(context);
//imgWeekView.SetAdjustViewBounds(true);
imgWeekView.SetScaleType(ImageView.ScaleType.Matrix);
layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.WrapContent,1f);
layoutParams.Height = 0;
imgWeekView.LayoutParameters = layoutParams;
Bitmap bitmap = Bitmap.CreateBitmap((int)xScreen, (int)yScreen, Bitmap.Config.Argb8888);
cnvWeekView = new Canvas(bitmap);
imgWeekView.SetImageBitmap(bitmap);
linearLayout.AddView(imgWeekView); //whole activity layout
//Test
cnvWeekView.DrawColor(new Color(128, 128, 128));
Paint paint = new Paint(PaintFlags.AntiAlias);
paint.Color = new Color(255, 255,0);
cnvWeekView.DrawCircle(50, 50, 40, paint);
Finally I found a way how to measure my ImageView and here I will post my answer.
I believed that there should be much easier solution, but maybe there isn't. From this question I took most of the important data:
How to get the width and height of an android.widget.ImageView?
Things look however a little different in my android application and I'm not experienced enough to tell why. I had to change things a little. I had to learn a bit about interfaces and this question helped too.
Implementing the View.IOnTouchListener interface
Here is how I combined things. First I created class that will do the measure.
public class MyPredrawListener : Java.Lang.Object, ViewTreeObserver.IOnPreDrawListener
{
ImageView imageView;
public MyPredrawListener(ImageView img)
{
imageView = img;
}
public bool OnPreDraw()
{
imageView.ViewTreeObserver.RemoveOnPreDrawListener(this);
int finalHeight = imageView.MeasuredHeight;
int finalWidth = imageView.MeasuredWidth;
Bitmap bitmap = Bitmap.CreateBitmap(finalWidth, finalHeight, Bitmap.Config.Argb8888);
imageView.SetImageBitmap(bitmap);
//Test to see result
Canvas cnv = new Canvas(bitmap);
Paint paint = new Paint();
paint.Color = new Color(255, 255, 0);
cnv.DrawColor(new Color(128, 128, 128));
cnv.DrawCircle(finalWidth-50, finalHeight-50, 50, paint);
return true;
}
}
And in code where I create my imageView I set the listener like this.
imgWeekView = new ImageView(context);
MyPredrawListener listener=new MyPredrawListener(imgWeekView);
imgWeekView.ViewTreeObserver.AddOnPreDrawListener(listener);
In OnPreDraw function I put test code to see the result graphically, clearing bitmap to gray color and painting yellow circle to bottom right of a view.

Applying Custom Color as background to excel table row using jxl

I have learned how to apply colours to background of excel table row using jxl library by using the below code
WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);
cellFormats.setBackground(Colour.AQUA);
but i dont know how to apply custom color as background
when i tried this Colour myColour = Colour(221,221,221); i got exception
WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);
Colour myColour = Colour(221,221,221);
cellFormats.setBackground(myColour);
can anyone please tell me some solution for this
first you need to create a method:
private final WritableCellFormat getCellFormat(Colour colour) throws WriteException {
WritableFont cellFonts = new WritableFont(WritableFont.ARIAL, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat cellFormats = new WritableCellFormat(cellFonts);
myColour= new Colour(10000, "1", 221, 221, 221) {
};
cellFormats.setBackground(myColour);
return cellFormats;
}
You need to:
change this:
Colour myColour = Colour(221,221,221);
cellFormats.setBackground(myColour);
to this:
myColour= new Colour(10000, "1", 221, 221, 221) {
};
and set in cell:
Label label1 = new Label(column, row, "text of cell or nothing", getCellFormat(myColour));
I hope helps.

Is there any way to fill a drawing in C# GDI+, where the drawing involves multile Arcs,Lines?

My application involves drawing of custom shapes, in which the details of the shapes are retrieved from the database. Shapes will be Lines, Arcs.
I will calling DrawArc and DrawLine function to draw the custom shapes.
My Problem is I need to fill this custom shape with a background color.
I am using the following namespaces.
using System.Drawing.Drawing2D;
using System.Drawing;
Things that I tried out:
Graphics.FillRegion Method
private void OnPaint(object sender, PaintEventArgs e)
{
Pen redPen = new Pen(Color.Red, 2);
// Create a graphics path
GraphicsPath path = new GraphicsPath();
// Add two lines, a rectangle and an ellipse
Graphics g = e.Graphics;
path.StartFigure();
path.AddLine(20, 400, 20, 20); // left
path.AddLine(20, 20, 400, 20); //top
path.AddLine(400, 20, 400, 400); // right
path.AddLine(400, 400, 20, 400); // bottom
path.CloseFigure();
g.DrawPath(redPen, path);
Region reg = new Region(path);
g.FillRegion(Brushes.Green, reg);
reg.Dispose();
g.Dispose();
}
Using this method it is easy to fill a simple shape, where the start and end points are clearly defined.
But my application involves complex shapes, which is difficult to track the points. If I use this method, filling of background color is not proper.
Pls suggest me some way of filling custom shapes.
thanks for your help.

Office Open XML: Drawing Dashed Line in Chart

Using office open XML SDK I have drawn a scatter chart with smooth line but wondering how to change the line style for the series to show dashed line?
Finally I figured it out using OPenXML Productivity tool. You have to change the PresetDash property to System.Dash.
private void MakeSeriesDashedLine(ScatterChartSeries scs)
{
C.ChartShapeProperties chartShapeProperties1 = new C.ChartShapeProperties();
A.Outline outline1 = new A.Outline();
A.PresetDash presetDash1 = new A.PresetDash() { Val = A.PresetLineDashValues.SystemDash };
outline1.Append(presetDash1);
chartShapeProperties1.Append(outline1);
scs.Append(chartShapeProperties1);
}

Javafx change PieChart color

I have a list of color representing a color sequence. I want to apply the new color sequence to the piechart data.
private final int CASPIAN_COLOR_COUNTS = 8;
public void setPieChartColor(PieChart chart, List<String> colors) {
chart.getData().get(i); // for debug to get the node name (.data)
/**
* Set Pie color
*/
int i = 0;
for (String color : colors) {
final Node node = chart.lookup(".data" + i);
node.getStyleClass().remove("default-color" + (i % CASPIAN_COLOR_COUNTS));
node.getStyleClass().add(color);
i++;
}
but all chart data take Only one color from Caspian color.
You can achieve custom pie colors in code using a method such as:
private void applyCustomColorSequence(
ObservableList<PieChart.Data> pieChartData,
String... pieColors) {
int i = 0;
for (PieChart.Data data : pieChartData) {
data.getNode().setStyle(
"-fx-pie-color: " + pieColors[i % pieColors.length] + ";"
);
i++;
}
}
Note that the method must be applied after the chart has been shown on an active scene (otherwise the data.getNode() call will return null).
Here is some sample code which uses it.
You can accomplish the same effect using css stylesheets.
For example a css stylesheet containing the following style definitions will change the default colors of a pie chart when the stylesheet is applied against a given chart.
.default-color0.chart-pie { -fx-pie-color: #ffd700; }
.default-color1.chart-pie { -fx-pie-color: #ffa500; }
.default-color2.chart-pie { -fx-pie-color: #860061; }
.default-color3.chart-pie { -fx-pie-color: #adff2f; }
.default-color4.chart-pie { -fx-pie-color: #ff5700; }
For an example of the stylesheet based approach: see the "Setting Colors of a Pie Chart" section of the Styling Charts with CSS tutorial.
The stylesheet approach has an advantage that the styles are separated from the code. It has the disadvantage that the colors are must be set the time the stylesheet are created rather than at runtime and the color sequence is restricted to a fixed number of colors (8).
In general, the stylesheet approach is recommended for most applications.
The css styles might not work if your values are negative. This might be a bug but I had to convert my values to positive values for the styles to work. When the values were negative all pies were white in color.

Resources