Xlsxwriter: how to disable text wrap in data label? - xlsxwriter

I'm generating pie charts using XLSX writer, and everything is going perfectly except for one thing: I can't seem to find a way to control the text from wrapping in my data labels (which contain percentage values). Any font size above a 10 causes the '%' symbol to wrap below the digits (you can see this in the image I've attached below). I'm trying to generate many charts, so adjusting them manually could be costly time-wise.
The docs suggest that wrapping can be enabled or disabled from num_format. For cells, this property is set with a 'format' object. However, while data label text has a num_format property, the docs explicitly state that it must be set with a string literal and cannot take a format object. I have no clue how to prevent string wrapping with a string literal format.
Alternatively, I've looked into expanding/reducing the width/height of the data labels. However, this option also seems to be missing from the library.
From what I can tell, none of the other properties seem to suggest a way to avoid wrapping text.
My question is, is there a solution I'm missing? I'll leave some of my output and code below.
Current Code:
chart.add_series({
'categories' : '={}!B1:C1'.format(product_sheetname),
'values' : '={}!B2:C2'.format(product_sheetname),
'data_labels':{
'percentage':True,
'fill': {'color':'#363636'},
'font': {'name':'Arial (Body)', 'color':'white', 'size':16},
},
'points' : [
{'fill':{'color':'#4471d2'}},
{'fill':{'color':'#ff871c'}}
]
})
Current Output:
UPDATE:
A quick fix per the comment below is to scale up the size of the chart in general. While this solves the wrapping issue, it defeats the purpose of using a larger font.

There isn't any way in XlsxWriter to adjust the size of the data label but Excel should do it automatically to get the best result.
I tried to replicate but didn't see the text wrap that you are getting (even though I tried 3 different versions of Excel):
import xlsxwriter
workbook = xlsxwriter.Workbook('chart_pie.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write_row('B1', ['Apple', 'Cherry'])
worksheet.write_row('B2', [53, 47])
chart = workbook.add_chart({'type': 'pie'})
product_sheetname = 'Sheet1'
chart.add_series({
'categories' : '={}!B1:C1'.format(product_sheetname),
'values' : '={}!B2:C2'.format(product_sheetname),
'data_labels':{
'percentage':True,
'fill': {'color':'#363636'},
'font': {'name':'Arial (Body)', 'color':'white', 'size':16},
},
'points' : [
{'fill':{'color':'#4471d2'}},
{'fill':{'color':'#ff871c'}}
]
})
worksheet.insert_chart('B3', chart, {'x_offset': 25, 'y_offset': 10})
workbook.close()
Output:

Related

Access Setting Theme VBA using MsoThemeColorSchemeIndex is off by 1

When setting a control/form theme, I often use VBA, as I can change the color on the fly. Works great. I have been trying to move my Database away from using hard-coded numbers, and built up a theme module that is included which has themes mapped so I could just change them there instead of everywhere else.
Then I realized, hey, there's an easier way to do this (or...so I thought).
Enter Enums MsoThemeColorIndex and MsoThemeColorSchemeIndex. This way, I can even do fancy things like decide that when I use myInternalTheme1 constant, I could just switch it out with MsoThemeColorSchemeIndex.msoThemeAccent1.
A long while back I noticed that if I used what is shown on the help docs (above), the themes were "off" by 1. Namely 5 (msoThemeAccent1 "Theme1"), instead of mapping to msoThemeAccent1 actually maps to "Theme2", and "Theme 2" Color is displayed. So, I just manually adjusted. But I'm wondering if I'm missing something here, and I'm using the value incorrectly?
I've used SaveAsText to export these forms after saving the values, and when I do, the field that's had "Theme 1" manually applied in Properties shows that the value 4 is used, which "should" map to msoThemeColorLight2, but doesn't.
Field's Backtheme setting:
BackThemeColorIndex =4
How I'm using this:
' In my "modColor"
Public Const MythemeAccent1 As Integer = 4 ' (help docs specify this as MsoThemeColorIndex.msoThemeColorLight2)
' This one colors the header in Theme2 "wrong color, correct enum value???"
' MsoThemeColorSchemeIndex.msoThemeAccent1 = 5 (as docs say)
Me.section(acHeader).BackThemeColorIndex = MsoThemeColorSchemeIndex.msoThemeAccent1
' This one colors the header with Theme1 "correct color, wrong enum value???"
' MsoThemeColorSchemeIndex.msoThemeLight2 = 4 (as docs say)
Me.section(acHeader).BackThemeColorIndex = MsoThemeColorSchemeIndex.msoThemeLight2
' my internal module, this works correctly (code looks correct, and correct color used).
Me.section(acHeader).BackThemeColorIndex = MythemeAccent1
If I assign the color in Design View via Form Properties > FormHeader > Format > Back Color > "Accent 1" it works correctly, and the theme is properly applied.
FormHeader Properties Format
I've verified the theme is correct, numerous times. I exported the theme, and verified that the XML for "Accent 1" is correct, and that "Accent2" is different. "Theme 1" is "Bluish" and "Theme 2" is "Redish" for reference, so it's not like my monitor rendering is just making me think it's different.
Excerpted XML from .Thmx file:
<a:accent1>
<a:srgbClr val="5C83B4"/>
</a:accent1>
<a:accent2>
<a:srgbClr val="E74B4B"/>
</a:accent2>

Birt export in pdf does not wordwrap long lines

My reports preview is ok.
But now, I need to export to pdf...and I've got an issue : the content of some cells are truncated to the witdh of the column.
For instance, 1 cell should display "BASELINE"...in the preview it's ok...but in pdf, it displays "BASEL".
I've been looking for a solution the whole day and did not find anything...
Of course : I don't want to fit the width of the column on the length of this word "BASELINE" because the content is dynamic...
Instead, I want to fix the column width and then, the cell should display something like that :
BASEL
INE
Any idea ?
Thanks in advance (am a little bit desperated...)
The solution is trivial in BIRT v4.9 if you've integrated the engine into your java code. Just set the PDF rendering options.
RenderOption options = new PDFRenderOption();
options.setOutputStream(out);
options.setOutputFormat("pdf");
options.setOption(PDFRenderOption.PDF_WORDBREAK, true);
options.setOption(PDFRenderOption.PDF_TEXT_WRAPPING, true);
task.setRenderOption(options);
You have to set a special PDF emitter option:
PDFRenderOption options = new PDFRenderOption();
options.setOption(PDFRenderOption.PDF_HYPHENATION, true);
This is if you integrated BIRT into your Java program.
For the viewer servlet, it is possible to set such options, too, AFAIK, but I don't know how; maybe on the URL or using environment variables.
I had the same issue. I found a very good topic about that : http://developer.actuate.com/community/forum/index.php?/topic/19827-how-do-i-use-word-wrap-in-report-design/?s=173b4ad992e47395e2c8b9070c2d3cce
This will split the string in the given number of character you want :
The function to add in a functions.js (for example). To let you know, I create some folder in my report project : one for the reports, one for the template, one for the libraries, another for the resources, I added this js file in the resources folder.
/**
* Format a long String to be smaller and be entirely showed
*
*#param longStr
* the String to split
*#param width
* the character number that the string should be
*
*#returns the string splited
*/
function wrap(longStr,width){
length = longStr.length;
if(length <= width)
return longStr;
return (longStr.substring(0, width) + "\n" + wrap(longStr.substring(width, length), width));
}
You will have to add this js file in the reports : in the properties -> Resources -> Javascript files
This is working for me.
Note: you can add this function in your data directly if you need only once...
The disadvantage of this : you will have to specify a max length for your character, you can have blank spaces in the column if you specify a number to small to fill the column.
But, this is the best way I found. Let me know if you find something else and if it's working.

Adding custom value to AmLegend valueText/periodValueText in AmXYChart

I am trying to plot line chart with AmXYChart instead of serial chart as I wanted to update all the line independently on a real-time basis. And also wanted to update valueText in legend for all line graphs dynamically.
I am able to get valueText and periodValueText in case of AmSerialChart but unable to get or set it in case of AmXYChart. Following is my legend setting in serial chart:
"legend": {
"valueWidth": 100,
"valueAlign": "left",
"valueText": "[[close]]",
"periodValueText": "[[value.close]]",
},
Any help will be appreciated, Either by amChart settings or by custom way.

Easeljs getBounds, when are dimensions set?

I'm importing someone's createjs application into an application loader which imports each developers application javascript to a global canvas. The application is working standalone but when I load the javascript dynamically I'm getting some strange behavior surrounding the getBounds() function.
I have some text created like this:
dialogText = new createjs.Text(text, 'bold ' + (28 * scale) + 'px Calibri', '#FFFFFF');
Then later on I use dialogText.getBounds() which returns null.
I try console logging dialogText and I can see that it is set and there is a value _bounds which reads null.
At what point do these values get set when the element in drawn to the canvas?
Note: I'm already adding the text to a createjs.Container() which has its bounds set before I run getBounds().
For a quick reference, I pasted EaselJS's update regarding getBounds():
(This dates back from "August 21, 2013", not sure if this is still necessary nowadays, but using setBounds(x, y, w, h) helped me with Shapes today [in 2017] actually!)
Source: http://blog.createjs.com/update-width-height-in-easeljs/
The _bounds property reflects a value that is manually set on the DisplayObject - which is helpful if you know the bounds of something that can't calculate it (like a Shape). It will always be null, unless you set it directly.
I tested your code, and it seems fine. Are you sure the scale or text values are not null? Maybe hard-code them to see if it works.
Do you have a sample somewhere? I noticed in the GitHub issue you posted that you mentioned it worked fine without RequireJS (though you don't mention that here). I would definitely ensure that the values are being set properly.
Here is the fiddle, as well as a simpler one with no EaselJS stage. Note that I have Calibri installed on my machine, so you might get different results if you are loading it in.
Sample code:
var dialogText = new createjs.Text("This is some text", 'bold ' + (28 * 1.5) + 'px Calibrix', '#FFFFFF');
console.log(">>",dialogText.getBounds());

Keen IO Chart Labels

I'm trying to figure out how to adjust the labels on an Area Chart visualization of my Keen IO data. I've looked through the available configuration options, but I'm not seeing what option would do this. Currently my chart just lists "null" on the legend on the right side of the chart, and on the hover tooltips when you hover over a particular peak. Just looking to switch it to list "Hits" instead of "null."
Does anyone know how/where I would configure those labels?
You can use labelMappingfor that. Something like:
client.draw(funnel, document.getElementById("chart"), {
library: "google",
chartType: "columnchart",
labelMapping: {
null: "Hits"
}
});