//-------------------------------------------------------------------// // // // MAKE TITLE // // // //-------------------------------------------------------------------// function make_title(chart_group, text_array, margins, canvas, maximum_drawing_space) { // Does chart title already exist? if (!chart_group.select("g.chart_title").empty()) { // Yes. Clear Title chart_group.select("g.chart_title").remove() // Reset Margins margins.title = charts_config.plot_attributes.title.size * 2 } // Construct full title var full_title = text_array.join(" ") // Store title lines var title_lines = [] // Max line length var characters_per_line = (canvas.x - margins.x.right) / (charts_config.plot_attributes.title.size / 2) while (full_title.length > 0) { var slice_position = characters_per_line - 1 var line_slice = full_title.slice(0, slice_position) var last_space = line_slice.lastIndexOf(" ") // space is first character, drop it if (line_slice[0] == " ") { full_title = full_title.slice(1, full_title.length) line_slice = full_title.slice(0, slice_position) last_space = line_slice.lastIndexOf(" ") } if (full_title[slice_position + 1] != " " & slice_position < full_title.length) { // the leading character of next splice is not a space (e.g. breaks a word) // and there is more in the title to come if (last_space == -1) { // no spaces in this line, we have broken a word line_slice = full_title.slice(0, slice_position - 1) + "-" slice_position -= 1 } else { // there is a space, truncate to that space slice_position = last_space line_slice = full_title.slice(0, slice_position) } last_space = line_slice.lastIndexOf(" ") } else if (slice_position < full_title.length & last_space < line_slice.length) { // last word is split, so add a hypen line_slice = full_title.slice(0, slice_position - 1) slice_position -= 1 last_space = line_slice.lastIndexOf(" ") // if the word is a two letter word, e.g. the last letter in the string is // the first letter of the two letter word, then that letter is droped for // a hypen before a space. That makes no sense, so drop the entire 2 letter word if (last_space == slice_position - 1) { // if space is last character drop it line_slice = full_title.slice(0, slice_position - 1) slice_position -= 1 } else { line_slice += "-" slice_position -= 1 } last_space = line_slice.lastIndexOf(" ") } if (last_space == slice_position) { // if space is last character drop it line_slice = full_title.slice(0, slice_position - 1) slice_position -= 1 } title_lines.push(line_slice) full_title = full_title.slice(slice_position, full_title.length) } var chart_title = chart_group.append("g").attr("class", "chart_title") for (var i = 0; i < title_lines.length; i++) { chart_title.append("text") .attr("class", "chartTitle") .text(title_lines[i]) .attr("text-anchor", "middle") .attr("font-size", charts_config.plot_attributes.title.size) .attr("font-family", charts_config.plot_attributes.title.family) .attr("x", margins.axes.y + margins.x.left + maximum_drawing_space.x / 2) .attr("y", margins.y.top + margins.buttons + (charts_config.plot_attributes.title.size * (i) + charts_config.plot_attributes.title.size / 2)) margins.title = chart_title.node().getBBox().height + charts_config.plot_attributes.title.size maximum_drawing_space.y = canvas.y - margins.y.top - margins.y.bottom - margins.axes.x - margins.title - margins.buttons } }