The KPop team has encoded a collection of group profiles for ten Korean pop bands, with discogrqphy data that categorizes albums in five different ways. This is encoded in an <album>
element with an @albumType
attribute, with six possible values, according their project Relax NG Schema:
album = element album {albumType?, xmlid?, text} albumType = attribute albumType {"mini" | "full" | "live" | "repackage" | "single" | "extended"}
Albums can be of the following types: mini, full, live, repackage, single, or extended. Our goal is to plot a stacked bar graph to show how the bands compare to each other in a selection of the album types they tend to release the most.
The KPop collection of group files is posted as a directory in our digitProjectDesign-Hub. Just like before when you accessed a collection of files, here is what you will need to do:
git pull
to update your local clone of the digitProjectDesign-Hub.Please be careful to copy rather the move the directory out of GitHub! If you move it out of the directory, the next time you sync our digitProjectDesign-Hub, GitHub will prompt you to commit the change and push it, which will effectively eliminate the Dickinson folder. I can easily put it back if that happens, but please alert me ASAP if something goes awry!
Our xsl:stylesheet
root element needs to indicate that we are outputting SVG in the SVG namespace (highlighted in purple below. The output method is set to XML. So you should set your xsl:stylesheet
and xsl:output
statements thus:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
xmlns="http://www.w3.org/2000/svg">
<xsl:output method="xml" indent="yes"/>
Open an XML file in the kpop-groups folder to work out the XPath you need to access the discography information. To plot an SVG stacked bar graph, we will be creating variables that hold information about the album types. In the graph we want to make, we want to create a set of stacked bars for each group representing its different album types. We can sort the output so that the bands are plotted from left to right ranging from the bands with the highest to lowest total count of albums. We will choose a different color for each of the album types we plot (and a color for the full count of albums). Our output should be labeled with a legend and a title, and the axes should be labeled, too.
For our sample solutions, we graphed the album releases in two ways:
You may plot all or any combination of album types you wish, and your output does not need to look exactly like ours, but it does need to represent the full count of albums for each band as well as at least two different album types.
You may, of course, also decide to scale and color your graph and bars differently than we did, and you will want to decide whether you prefer to use the SVG <rect>
element or the SVG <line>
element (which is what we used in our solution). While we leave it up to you to decide how you want to scale and color your graph or the fonts you want to use for your text, here are some guidelines and suggestions:
collection()
function. Here is what we used:
<xsl:variable name="kpopGroupColl" select="collection('kpop-groups/?select=*.xml')"/>
<g>
(group) element in the upper right quadrant, to set our X axis and Y axis to meet at x=0 and y=0, and to plot all our y values to start from a zero base point at the X axis and move up the screen into negative numbers. We then use the <g>
element to shift this a little to the right and down into visible and workable coordinate space on the display monitor, by simply setting the @transform
attribute with the translate()
function. To do this:
<g>
start tag as <g transform="translate(80, 500)">
will shift it to the right 80 pixels and down by 500 pixels (the first number is the amount to shift on the X axis and the second is the amount on the Y axis). You can hard-code the number of pixels to shift down, but a more elegant approach is to use XPath to calculate the height of the tallest bar and employ that calculation in setting the value for the shift so your full plot will be visible in a browser window.album albumType="single"
) that you want to sit at the bottom of your stack, plotting upwards from the X axis and check to see that you are getting good results in a web browser. To plot the other rectangles (or thick lines) on top of this, you will need to start drawing those from the top of a bar you have just drawn, which you do by adding the Y value for the top of the low bar to the height of the next bar that is to sit on top of it.text
element formatting, and what attributes are used to control it. (There are a number that you might experiment with to rotate text and change its orientation, but we used the @style="writing-mode: tb;"
attribute to make the text run vertically down the page.) Take a look at Jakob Jenkov's SVG tutorial for a good resource for learning about the range of attributes you can apply to SVG text
and other SVG elements, and the various things they do. We find the @text-anchor
attribute especially helpful to anchor the text’s X and Y coordinates either in the start, end, or exact center of the text string.<xsl:sort>
so that the bands are plotted in sequence based on their counts of albums. (Our sample plot goes from highest-releasing bands to lowest-releasing bands.)Turn in your XSLT file as well as your output SVG file. Remember to save and open your SVG output in <oXygen/> and in a web browser to be sure it is valid and that it is rendering as you think it should be.