Diagonal stripes and bars
You could use a triangle on :after
:pseudo-element for the first image and linear-gradient
for the second image.
To get the cuts, the part of the linear-gradient
have been set to transparent
.
body { background: url(http://www.lorempixel.com/600/400/sports/) no-repeat; width: 100%; height: 100%; margin: 0;}div { margin: 10px;}#one { position: relative; width: 200px; height: 50px; background: #324660; color: #F5F4F4; font-size: 17px; text-align: center; line-height: 50px;}#one:after { content: ''; position: absolute; width: 0; height: 0; right: -39px; top: 0; border-top: 50px solid #324660; border-right: 40px solid transparent;}#two { position: relative; width: 500px; height: 50px; background: linear-gradient(135deg, #324660, #324660 56%, #F96620 56%, #F96620 66%, #D1D7DB 66%, #D1D7DB 68%, transparent 68%, transparent 83%, #D1D7DB 83%, #D1D7DB 85%, #F96620 85%); color: #F5F4F4; line-height: 50px; text-align: left; padding-left: 20px;}span { font-size: 17px;}
<div id="one">1. Marketing:</div><div id="two"><span>Benefits</span> for facilities, therapists and patients</div>
How do I create and distribute diagonal stripes on a rectangle?
Using a gradient fill to draw lines
On trashgod's tip I tried filling a shape with a gradient that had sharp edges to simulate line drawing. This would prevent a lot of calculations and could potentially be a lot simpler. It worked quite ok for thick lines, but not for thinner lines. Using the following code produces the fill in the first picture:
rect.setSpace(spaceBetweenLines);
Color bg = Color.YELLOW;
Color fg = Color.BLUE;
rect.setPaint(new LinearGradientPaint(
(float) startX, (float) startY, (float) (startX + spaceBetweenLines), (float) (startY + spaceBetweenLines),
new float[] {0,.1f,.1001f}, new Color[] {fg,fg,bg}, MultipleGradientPaint.CycleMethod.REPEAT)
);
Drawing lines using graphic primitives
Although simpler it did not work in my case. The more elaborate, but to me, more natural way of doing it, is simply drawing lines on top of the shape (rectangle, cirle, ...). The following code was used in producing the second image. Observe the use of the clip(Shape s)
to restrict the line drawing to the shape underneath. The reason for not simply drawing a rectangle and using clip()
to limit the shape is that the clip()
operation is not aliased, thus producing jaggies. Therefore I have to draw the shape first to get smooth edges, then set the clip to prevent overflow in the forthcoming line drawing, and finally draw the lines.
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setPaint(getBackground());
g2.fill(getShape());
g2.setClip(getShape());
// draw diagonal lines
g2.setPaint(getLineColor());
for (int x = (int) this.x, y = (int) (this.y); y - height < (this.y + height + getSpace()); ) {
g2.drawLine(x, y , x + (int) width , y - (int) width);
y += getSpace();
}
CSS Diagonal Lines with filled space
Diagonal line with a solid color under the line...
html
<div class="uguu"></div>
css
.uguu {
line-height:0;
width:0;
border-top:20px solid transparent;
border-right:100px solid green;
}
fiddle: http://jsfiddle.net/83Wyy/
How can I add hatches, stripes or another pattern or texture to a barplot in ggplot?
One approach is to use the ggpattern
package written by Mike FC (no affiliation):
library(ggplot2)
#remotes::install_github("coolbutuseless/ggpattern")
library(ggpattern)
ggplot(data = df, aes(x = Class, fill = StudyTime, pattern = Nerd)) +
geom_bar_pattern(position = position_dodge(preserve = "single"),
color = "black",
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.1,
pattern_spacing = 0.025,
pattern_key_scale_factor = 0.6) +
scale_fill_manual(values = colorRampPalette(c("#0066CC","#FFFFFF","#FF8C00"))(4)) +
scale_pattern_manual(values = c(Nerd = "stripe", NotNerd = "none")) +
labs(x = "Class", y = "Number of Students", pattern = "Nerd?") +
guides(pattern = guide_legend(override.aes = list(fill = "white")),
fill = guide_legend(override.aes = list(pattern = "none")))
The package appears to support a number of common geometries. Here is an example of using geom_tile
to combine a continuous variable with a categorical variable:
set.seed(40)
df2 <- data.frame(Row = rep(1:9,times=9), Column = rep(1:9,each=9),
Evaporation = runif(81,50,100),
TreeCover = sample(c("Yes", "No"), 81, prob = c(0.3,0.7), replace = TRUE))
ggplot(data=df2, aes(x=as.factor(Row), y=as.factor(Column),
pattern = TreeCover, fill= Evaporation)) +
geom_tile_pattern(pattern_color = NA,
pattern_fill = "black",
pattern_angle = 45,
pattern_density = 0.5,
pattern_spacing = 0.025,
pattern_key_scale_factor = 1) +
scale_pattern_manual(values = c(Yes = "circle", No = "none")) +
scale_fill_gradient(low="#0066CC", high="#FF8C00") +
coord_equal() +
labs(x = "Row",y = "Column") +
guides(pattern = guide_legend(override.aes = list(fill = "white")))
Related Topics
Css3 Transform Difference in Firefox and Chrome and Ie
How to Get Scale Works Cross-Browser in Center Position on Svg Elements with CSS
In CSS, How Does Overflow Interact with Float
Less Class Name String Interpolation Not Working
Firefox 4 Not Rendering A:Visited::Before (Except Color)
Why Is the Descender "Created" When Baseline Is Set to Vertical-Align
CSS - Increasing Size of Holder or What Ever Its Called
Sass --Watch with Automatic Minify
How Do Browsers Deal with "Tofu" Characters
Right Border of the Addthis Counter Missing with Twitter's Bootstrap 3
Differencebetween CSS and SCSS
Bootstrap 3 Panel Header with Buttons Wrong Position
Updated CSS Stylesheet Not Loaded Following Deployment to Heroku
Media Query for High Resolution Mobile 1080Px (Xperia Z etc)