Jump to content

Module talk:Piechart

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Option not to sum to 100 percent

[edit]

Would be nice to have an "other" chunk that isn't mentioned in the legend. Wizmut (talk) 22:00, 4 February 2024 (UTC)[reply]

@Wizmut You can see an example for "other" here: Template:Latest pie chart of world power by source. Though this only makes sense if you have percentages as values. It might be a bit harder to update charts this way. Depends on whether the source has percentages or absolute values. Nux (talk) 02:50, 24 January 2025 (UTC)[reply]
I'm not sure how this example applies. The other chunk is mentioned in the legend, the opposite of what I asked for. Wizmut (talk) 03:36, 24 January 2025 (UTC)[reply]
@Wizmut, for better accessibility, any value in the pie chart should be in the legend. I think the linked example is rendering correctly, Rjjiii (talk) 07:45, 25 January 2025 (UTC)[reply]

Combining with Template:Pie chart

[edit]

This has some advantages over {{Pie chart}} (tooltips, calculates percentages, no crosshair glitches). I've been tinkering with using the template's pie chart as a replacement for the one in {{Pie chart}}. There are a bunch of demos of {{Pie chart/sandbox2}} at Template:Pie chart/testcases. Nux, I have a few questions, is this something that you'd want to get wider usage or more a proof of concept. It's only transcluded to a handful of pages right now.

Also, the module should do something to sanitize user input for titles. A lot of people will place links there so the brackets ([ and ]) need to be checked for, and in case they use a piped link, it also needs to remove the pipe (|) and the wikilink target. Rjjiii (talk) 21:03, 22 January 2025 (UTC)[reply]

Hi, yes this module can be used widely. I've added suport for links in labels and added an example here: Module:Piechart#Links. I think now in most cases you should be able to replace {{Pie chart}}, maybe even converting it automatically to the {{Piechart}}. Also added support for floating graphs in the template. Nux (talk) 02:45, 24 January 2025 (UTC)[reply]
Fantastic! I checked the link out above and it looks clean. When you mention "converting it automatically" do you mean as a kind of wrapper template that could just feed the parameters from existing transclusions to {{Piechart}}? Rjjiii (talk) 08:48, 24 January 2025 (UTC)[reply]
@Nux I've gone ahead and made sandbox copies for the module and template. I also started Template:Piechart/testcases and am checking for any bugs. If you look at "verbose", the colored bullets in the legend need "min-width" set somewhere. On mobile, the live template as a "thumb" moves to the left instead of the center. It looks like adding "display:flex" to "div.pie-thumb" should fix it (visible in the sandbox). Also, I tried applying the same styling to the caption as in Template:Pie chart. That template can likely be made into a wrapper for this one, Rjjiii (talk) 07:44, 25 January 2025 (UTC)[reply]
@Rjjiii I've fixed the bullets. Added some more tests. Some of those tests don't work the same as on plwiki, but should be enough to keep things compatible. Nux (talk) 11:43, 26 January 2025 (UTC)[reply]
Awesome! What do you think about centering the "thumb" charts on mobile, and styling the caption similar to {{pie chart}}? Also, what's different on plwiki? Rjjiii (talk) 19:49, 26 January 2025 (UTC)[reply]
Personally I wouldn't want to use thumb at all (I've only added that option for compatibility). There are a few problems with that... Default layout of Wikipedia is now quite thin and obviously even thinner on mobile device (which is most of the audience of Wikipedia these days). So personally I think that doing floats is mostly a thing of the past. Charts like that should just be part of the hypertext, with images and media flowing along with text (which also means less problems with a long infobox pushing things down and outside of intended spot).
Another problem is using styles from Wikipedia. Just now there were changes in structure of images. It's generally not advised to use them. So it's probably best to write CSS here: Template:Piechart/styles.css (not in the module). Nux (talk) 20:43, 26 January 2025 (UTC)[reply]
I've moved styles over to Template:Piechart/sandbox/styles.css. Regarding, "So personally I think that doing floats is mostly a thing of the past." yeah absolutely. I'm just trying to get the best backwards compatibility with {{Pie chart}}. In truth, I was surprised when you took the initiative to move the thumb feature into the module, but good surprised like getting a third thigh in a Popeye's two-piece. Those style changes are minor and shouldn't affect the non-thumb output. Also, before I start a discussion at {{Pie chart}} about converting that template, do you want to apply for template editor rights on en-wiki? Rjjiii (talk) 04:39, 28 January 2025 (UTC)[reply]
Thanks for the suggestion. Will apply in a minute. Nux (talk) 20:30, 29 January 2025 (UTC)[reply]
Hi. Another user opened a discussion here: Wikipedia:Templates for discussion: Template:Piechart. Nux (talk) 23:26, 30 January 2025 (UTC)[reply]
@Nux, that discussion has now closed as merge. I read the outline at Template:Pie chart/sandbox, and you seem to have this mostly planned out. I tested the sandbox template against the live template on some different browsers, screen sizes, dark mode, and so on. For all of the places where the rendering is different, this module seems to offer better readability (for example the outline color on the bullets changes in dark mode and the text does not become small/grey on mobile). I have been fixing the bogus parameters used in transclusions of the older, widely used template {{pie chart}}. When you get closer to merging, feel free to reach out for a hand with writing/updating documentation, testing anything, feedback, etc. So far, it's looking good, Rjjiii (talk) 02:39, 26 February 2025 (UTC)[reply]
@Rjjiii hi, thanks for the ping and the offer to help. Even thought I've merged the template, there is still probably a lot of typos and inconsistencies in the docs. Please have a look Template:Pie chart/doc. Can you also double check everything? I think Template:Piechart can now be redirected to the Template:Pie chart. But there's some other stuff which maybe needs a redirect or removal... or merge? I guess maybe you could add more testcases at the end of Template:Pie chart/testcases? Nux (talk) 02:38, 27 February 2025 (UTC)[reply]
I'll try to look over the /doc documentation soon.
Yes, it makes sense to redirect Template:Piechart, and since the subpages all have a corresponding page in the merged template, I think they should probably all become redirects (Template:Piechart/sandboxTemplate:Pie chart/sandbox and so on).
By "add more testcases" do you mean merging over the examples on Template:Piechart/testcases or do you have something else in mind? Rjjiii (talk) 03:00, 27 February 2025 (UTC)[reply]
@Rjjiii yeah, I was thinking about merging test cases. You've created most of the proper test cases for the module IIRC, so I was thinking you might want to do the honours :). And also to be honest I'm not sure what are the procedures here on en.wiki. On pl.wiki we have a simpler structure and don't use sandboxes that much. Nux (talk) 09:06, 27 February 2025 (UTC)[reply]
@Nux, I made a lot of structural changes to Template:Pie chart/doc while keeping most of the details, and I brought over much of the info from Module:piechart/doc. It now starts with an overview of the different ways that the template can work, and presents enumerated parameters first because (at the moment) nearly all transclusions use that format. If there is anything that I've gotten wrong, feel free to fix it.
While testing, I ended with a couple of questions:
  1. What is the use case for the "minimalistic" version with no legend? I'm guessing it is meant to be used in another template or a table of something? I think whatever the point of allowing that format, the /doc page should give a usable example.
  2. Can the non-floating "thumb=none" format be used with enumerated parameters? Right now, the template can give usable output with "value1=" and "thumb=none", but it has some odd quirks that are inconsistent with other uses. If the template isn't meant to be used this way, it should probably emit an error message rather than a chart.
I'll start on the testcases soon, Rjjiii (talk) 19:39, 1 March 2025 (UTC)[reply]
It looks good, thanks :). I'm more in favour of JSON-like array syntax, as I find it more convenient long term, but I can understand your point of view. Nux (talk) 20:04, 1 March 2025 (UTC)[reply]

Problems with invalid params

[edit]

It's great that there is a new module but I need to report that some articles are in Category:Pages with script errors due to recent changes. For example, Karwar#Demographics currently shows "Lua error: mw.text.jsonDecode: Syntax error." That may well be due to bad wikitext in the article, but it would be better if the module gave an error message. I started to have a look at what the issue might be but Module:Piechart needs some general work before looking any further. I will have a go at tweaking Module:Piechart/sandbox so whitespace and global variables match the general style (tabs for indents and no globals). Unfortunately that will generate a huge diff from the current module, but it has to be done sooner or later.

Johnuniq (talk) 07:18, 27 February 2025 (UTC)[reply]

I cleaned the whitespace and fixed the globals. The diff is already large and I'll leave my temporary fix (forward declarations) in the sandbox until this has been considered. Johnuniq (talk) 07:50, 27 February 2025 (UTC)[reply]
Experiments show some wikitext that results in errors. Karwar#Demographics has |right and Template:Pie chart of New Zealand vineyard area by region has |thumb. Perhaps they are invalid parameters or haven't been implemented? Zagatala District#Ethnic groups has some misplaced text as parameter 1. Johnuniq (talk) 08:23, 27 February 2025 (UTC)[reply]
Not oppose to some cleanup, but please don't move functions inside other functions. That makes the code less readable for me and might also break some of my testing habits I use. I also don't see the point. Why would you want to redefine a function each time you call a method. Nux (talk) 09:23, 27 February 2025 (UTC)[reply]
If you really must I would rather have all functions assigned to p as method instead of moving them inside. Nux (talk) 09:25, 27 February 2025 (UTC)[reply]
Also I don't know why are you calling them globals. Those are local functions private to the module. I've just double checked if I didn't messed up something and it doesn't seem like I did. I cannot even call trim("a ") in a debug console. I get an error instead:

Lua error in console input at line 7: attempt to call global 'trim' (a nil value).

So it's clearly not a global function. Otherwise I would be able to call it from the debug console, right? Nux (talk) 09:51, 27 February 2025 (UTC)[reply]
Fixed. It turned out that the problem was in the template, not the module. To be more exact, the call to {{Pie chart}} was invalid, as you mentioned, but I modified the template so it is resilient to problems like that. The article's wikitext should be modified, but in the meantime, it will work as it is. Nux (talk) 10:17, 27 February 2025 (UTC)[reply]
Thanks for the fix. I'll post some thoughts on the module that I prepared while you were doing that. I agree that embedded functions are ugly, but I have got used to them because they have some advantages. However, simply moving the function before where it is used will also fix the global issue. Functions in p should only be those that are intended to be used in external modules/templates. I know that some modules put everything in p but that invites trouble because it is impossible to know what might happen if one of the helper functions is modified. In the future, Module:Piechart may be invoked by another module. If the invoking module uses require('strict'), invoking a module with a global variable causes a crash. I put an example in Module:Sandbox/Johnuniq/temp with the result shown on Module talk:Sandbox/Johnuniq/temp. With require('strict') the talk page shows "Lua error in Module:Piechart at line 254: assign to undeclared variable 'sumValues'." That is the first global variable encountered when the module is compiled. Line 254 is function sumValues(data) but that is equivalent to sumValues = function(data)...end which explains the strange error message (it assigns a function to sumValues). I have to go now but will return in a day or two. Feel free to experiment in my sandbox. If you comment out the first line, pasting Module talk:Sandbox/Johnuniq/temp in "Preview page with this template" then clicking Preview will show that the talk page gives the correct result, namely "(abc)". That demonstrates that trim is a global function. I don't recall how the debug console works. It might allow access only to exported functions. Johnuniq (talk) 10:36, 27 February 2025 (UTC)[reply]
Thanks for the updates. The only remaining global is entryJson and that variable can probably be eliminated. Johnuniq (talk) 03:12, 28 February 2025 (UTC)[reply]
 Fixed Oh, yeah. I missed that. Thanks. Nux (talk) 22:20, 28 February 2025 (UTC)[reply]

Doesn't handle Template:Efn placed within value

[edit]

On List of power stations in Arizona and similar pages for other US states, this template automatically replaced the old Template:Piechart. That template allowed for footnotes (Template:Efn) to be placed within a value in the chart, but it appears that messes up the formatting for this type of pie chart. You can see the issue on the page I linked. I am hoping that this issue won't be too difficult to address or that there may be some simple alternative or workaround. – OdinintheNorth (talk) 21:37, 3 March 2025 (UTC)[reply]

Efn test
  1. First 600 (60.0%)
  2. Second 300 (30.0%)
  3. Third[a] 100 (10.0%)
There are two problems, as shown in the demo. The first is that the note "[a]" is split over three lines. The second is that holding the mouse cursor over the third slice of the pie chart shows
Third◆'"`UNIQ--ref-00000002-QINU`"'◆ 100 (10.0%)
where the two ◆ characters are actually byte 127 (ascii rubout). That is a strip marker passed to the module but not processed after return from the module. If support for embedded references is wanted, an ugly trick from Module:Convert can be used. I can't work out why "[a]" has a problem. Maybe it would go away if the strip marker issue was fixed? Johnuniq (talk) 23:53, 3 March 2025 (UTC)[reply]
Templates are not really supported in labels. There is some effort to make simple wikitext work in labels, but only wiki-links are truly supported. Having said that I did fix CSS so that it looks more decent. Nux (talk) 01:20, 4 March 2025 (UTC)[reply]
Amazing, I wondered why it suddenly worked. Do you want the second problem fixed? Johnuniq (talk) 01:35, 4 March 2025 (UTC)[reply]
Thank you very much for quickly addressing this issue Nux! And thank you Johnuniq for explaining this far more clearly. I'll be honest, I didn't even notice the related hovertext bug. I really don't think that one is much of a problem at all and for my purposes I'm considering this fixed. – OdinintheNorth (talk) 02:27, 4 March 2025 (UTC)[reply]
  1. ^ Dummy values.