Modify the c3 axis labels using CSS

I've been using the c3 package in R, which serves as a wrapper for the C3 javascript charting library created by Masayuki Tanaka.

One issue I encountered is that my c3 chart was cutting off the last date on the x-axis. After inspecting it in Chrome Inspector, I manually adjusted the text-anchor: middle to text-anchor: end

This workaround seemed to resolve the problem:

However, I'm unsure how to achieve this through CSS styling alone.

Below is a snippet of my R script along with the relevant CSS code. While I believe the issue lies more within the CSS aspect, here are the details:

R Script

library(shiny)
library(c3)

data <- data.frame(a = abs(rnorm(20) * 10),
                   b = abs(rnorm(20) * 10),
                   date = seq(as.Date("2014-01-01"), by = "month", length.out = 20))

ui <- fluidPage(
  
  includeCSS("c3.css"),
  
  c3Output("chart",  height = "100%")
)

server <- function(input, output, session) {
  output$chart <- renderC3({
    data %>
      c3(x = 'date') %>
      tickAxis('x', count = 5, format = '%Y-%m-%d') 
  })
}

shinyApp(ui, server)

CSS Styling

.c3-line {
  stroke-width: 4px !important; 
}

.c3-axis-x-label {
    text-anchor: end; 
}

Answer №1

No code has been provided here, but the following snippet may be helpful:

.c3-axis-x g.tick:nth-last-child(3) text {
  transform:translateX(-5%);
}

Ensure you target the text correctly and adjust the value as needed.

Update: The correct selector is not :last-child - revised based on actual code. Make sure to target your selector accurately and apply the transform property.

Update 2: Turns out it was the group .c3-axis-x that contains the tick texts. Code has been updated once again.

Answer №2

Something that comes to mind right away:

Your current CSS is only targeting the 'date' element. The other 'text' element does not have a class, so the CSS for '.c3-axis-x-label' will only affect the first text element.

Try adding the 'c3-axis-x-label' class to your other 'text' element as well.

Another point to consider:

The 'text' tag you want to modify has its styles written within the tag https://i.stack.imgur.com/2nwww.png

When styles are set this way, they will always take precedence over applied CSS.

One solution I can suggest is to use !important in your CSS to override the inline style. I generally advise against using !important, but if it's too challenging to directly modify the CSS, this could be an option.

If you can assign a class to the other text element, try applying styles to 'text' in your CSS:

g > text {
   text-anchor: end; 
}

Let me know if any of these suggestions were helpful. If not, providing some code would be beneficial in assisting you further.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The password-protected HTML blog remains hidden until the correct entry is entered into the password box, causing

After successfully creating a password redirect page where entering the correct password redirects you to another URL (psswrdtest.tumblr.com with the password as correctpsswrd, redirecting to google.com*), I attempted to improve it by making the password p ...

jQuery Accordion not showing up on screen

On my FAQ page, I have integrated a jQuery Accordion with a repeater control nested inside to display various questions and answers. The FAQ page utilizes a master page named LaunchPage.Master. Here is the code for LaunchPage.Master: <html> <he ...

The alignment issue persists in HTML/CSS despite troubleshooting efforts

I am facing a challenge while attempting to center text within a modal window, despite my efforts the text remains uncentered. This is my HTML code: <div ng-init="modalCompassDir()"> <div class="myModal"> <img class='floor ...

What is the best way to target an HTML element that is only connected to a particular class?

My HTML code includes the following 3 elements: <span class="a b"></span> <span class="a"></span> <span class="a b"></span> I am trying to select the element that only has the class "a". When I use $("span.a"), it sele ...

List arranged in order with a hyperlink in the center and an image positioned on the right side

I am trying to create an ordered list with a link to an article in the middle and a small png image file positioned directly to the right of it. For reference, here is an image depicting the exact type of list that I am aiming to achieve: https://i.stack. ...

A helpful tip for creating line breaks within a Vue JS v-for loop using CSS

I am looking to arrange the names in alphabetical order when a list item is clicked. My tools of choice are Vue.js and Flex Grid. The list item I am working with is called ListAll, When clicking on ListAll, I want to display all the records grouped by na ...

Is there a way to showcase a row of images when a button is clicked?

I am looking to create a functionality where pressing one of the buttons shown in the image below will toggle visibility of specific sections containing 3 images each. For example, clicking on "Tapas" will only display tapas images and hide main course ima ...

Revamp the layout of the lower HTML video menu

Here is my code: <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> video { width: 100%; height: auto; } </style> </head> <body> <video width="400" controls> ...

Issue with Wordpress Submenu Items not Displaying

I've included sub menu items in my primary navigation, but they're not visible at all? I've examined the css and haven't found any code that would hide the sub menu. ul.art-hmenu { display: inline-block; vertical-align: midd ...

Choose tag using position in a sequence

I am working on updating the label text "E-mail" to say "User Name" in this HTML markup. The only tool I have access to for this task is jQuery. <div class="foo" style="border:1px solid #444"> <span class="field-with-fancyplaceholde ...

What causes the lower gap to be smaller than expected when using "top: 50%; translateY(-50%);" on specific elements within the parent container?

My top-bar layout needs to have all elements vertically centered, but I'm experiencing issues with the top-bar_right-part not behaving as expected when using the core_vertical-align class. The left menu works fine, however. To illustrate the problem, ...

Adaptable website design featuring dynamic data grid

I've spent quite some time searching for a way to achieve the layout displayed in the linked image, but I haven't been successful in finding exactly what I need. My goal is to create a responsive layout that includes a datagrid (ui-grid) within ...

Is it possible to use only CSS to determine if text has wrapped and apply different styles to it?

Let's say we have a button with lengthy text: [Click here to find out more] Due to its length, the text may be split on smaller screens like this: [Click here to find out more] The goal is to align the text to the left when wrapped and to the ce ...

Floating dropdown within a scrolling box

How can I ensure that the absolute positioned dropdown remains on top of the scrollable container and moves along with its relative parent when scrolling? Currently, the dropdown is displayed within the scrollable area. The desired layout is illustrated i ...

Attempting to remove an attribute or property in JavaScript will not yield the desired result

When I try to close the menu after opening it, the inline style won't get removed despite trying different methods. The CSS only has text properties for alignment and the class can-transform contains a media query. That's all the information I h ...

There seems to be a problem with the while loop in the PHP code while creating a forum

Encountered a new error that says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND type='o'' at line 1. Can someone assist in fixing this issu ...

Using jQuery to load the center HTML element

So here's the plan: I wanted to create a simple static website with responsive images that load based on the browser width. I followed some suggestions from this link, but unfortunately, it didn't work for me. I tried all the answers and even a ...

Slideshow through each item using Typescript and Angular8

I came across a solution in this carousel on by one link, but I'm struggling to work with the jQuery code even though I have JQuery installed in my project. For example: const next = jQuery(this).next(); I am looking to convert the JQuery code from ...

Is it possible to have 3 divs with the height of the tallest

I have a unique template that I employ for crafting responsive websites. It relies on boxes with percentage widths, floats, and clears. A prime instance can be viewed via this link. Notice how the three boxes vary in height. While it's simple to set a ...

Is it possible to alter the background color once the content of an input field has been modified?

I am working with an angular reactive form and I want to dynamically change the background color of all input fields when their value is changed. Some of these input fields are pre-populated and not required. I came across a potential solution on Stack Ove ...